?
# | Author | Problem | Lang | Verdict | Time | Memory | Sent | Judged | |
---|---|---|---|---|---|---|---|---|---|
83279171 |
Practice: SarthakBansal |
191A - 32 | GNU C++11 | Accepted | 278 ms | 260 KB | 2020-06-10 10:48:37 | 2020-06-10 10:48:37 |
#include<bits/stdc++.h> using namespace std; #define ll long long #define f(i,a,b) for(ll i=a;i<b;i++) void solve() { int n;cin>>n; int dp[26][26]; f(i,0,26) { f(j,0,26) dp[i][j]=0; } f(i,0,n) { string s;cin>>s; int r=s[s.length()-1]-'a';//right last char int l=s[0]-'a';// left phla char // AIM: GO FROM J TO R // Path 1: go from j to r // Path 2: go from j to l then l to r ONLY IF There exists a path between j to l. f(j,0,26) { if(dp[j][l]!=0) dp[j][r]=max(dp[j][r],dp[j][l]+(int)s.length()); } dp[l][r]=max((int)s.length(),dp[l][r]); } int ans=0; f(i,0,26) { ans=max(ans,dp[i][i]); } cout<<ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ll t=1,caseno=1; // cin>>t; while(t--) { //cout << "Case # " << caseno << ": "<<endl; solve(); } cerr << "Time elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n"; }
?
?
?
?