191A - Dynasty Puzzles - CodeForces Solution


dp *1500

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define dbg(x) cerr << #x << " " << x << "\n";
int main() {
    ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
    ll n; cin >> n;
    vector<vector<ll>> dp(27, vector<ll>(27, -1));
    for(ll i=0; i<n; i++) {
        string s; cin >> s;
        ll fst = s[0] - 'a';
        ll lst = s.back()-'a';
        ll len = s.length();
        for(ll j=0; j<26; j++) {
       
            if(dp[j][fst] != -1) {
                dp[j][lst] = max(dp[j][fst] + len, dp[j][lst]);
            }
        }
        dp[fst][lst]=max(dp[fst][lst],len);
    }
    ll ans = 0;
    for(ll i=0; i<26; i++) {
        ans = max(ans, dp[i][i]);
    }
    cout << ans << "\n";
}


Comments

Submit
0 Comments
More Questions

1475A - Odd Divisor
1454B - Unique Bid Auction
978C - Letters
501B - Misha and Changing Handles
1496A - Split it
1666L - Labyrinth
1294B - Collecting Packages
1642B - Power Walking
1424M - Ancient Language
600C - Make Palindrome
1669D - Colorful Stamp
1669B - Triple
1669A - Division
1669H - Maximal AND
1669E - 2-Letter Strings
483A - Counterexample
3C - Tic-tac-toe
1669F - Eating Candies
1323B - Count Subrectangles
991C - Candies
1463A - Dungeon
1671D - Insert a Progression
1671A - String Building
1671B - Consecutive Points Segment
1671C - Dolce Vita
1669G - Fall Down
4D - Mysterious Present
1316B - String Modification
1204A - BowWow and the Timetable
508B - Anton and currency you all know