前言
思路
字符串拼接 首尾相连 问最大长度
直接dp
Mycode
// Problem: CF191A Dynasty Puzzles
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/CF191A
// Memory Limit: 250 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)
//��һ�δ��Ϻ�վ֮�� ��Ϊʹ��#define int long long TLE���¿��˺ܾõ�ʱ�� ���˼���ģ��
//����ʹ�ø��ӵ�ģ��
#include <iostream>
#include <vector>
#include <map>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
#define IOS ios::sync_with_stdio(false);
#define CIT cin.tie(0);
#define COT cout.tie(0);
#define ll long long
#define x first
#define y second
#define pb push_back
#define endl '\n'
#define all(x) (x).begin(),x.end()
typedef priority_queue<int,vector<int>,greater<int>> Pri_m;
typedef pair<int,int> pii;
typedef vector<int> VI;
map<int,int> mp;
const int N = 27;
int f[N][N];
char s[10];
void solve()
{
int n;
cin>>n;
for(int i = 1;i<=n; i ++ ){
scanf("%s",s);
int len = strlen(s);
int l = s[0] - 'a' , r = s[len-1] - 'a';
for(int j = 0 ;j<26;j++){
if(f[j][l]) f[j][r] = max(f[j][r],f[j][l] + len);
}
f[l][r] = max(f[l][r],len);
}
int ans =0 ;
for(int i=0;i<26;i++)
ans = max(ans,f[i][i]);
cout<<ans<<endl;
}
/**mYHeart is my algorithm**/
int main()
{
//int t;cin>>t;while(t -- )
solve();
return 0;
}