大意是:给你nn个字符串,将这些字符串拼接,求一个最长的序列,使这个其中每个串的最后一个字母与第一个字母相同(最后一个串的最后一个字母与第一个串的第一个字母相同),而且后面的串只能接在前面的串的后边。

 f[ch][ch] 首尾为ch , 的最大长度

 

 对某个 串s , f[j][y]= max{ f[j][x]+length }

 

#include <iostream>
#include<queue> 
#include <cstring>
#define IOS std::ios::sync_with_stdio(0)
using namespace std;
 const int N =5e5+4;
 char s[N];
 int n,f[27][27]; 
 void sov(){
 	int i,j,len;
 	
 	cin>>n;
 	
 	for(i=1;i<=n;i++){
 		cin>>s+1; len=strlen(s+1);
 		int x=s[1]-'a',y=s[len]-'a';
 		
 		for(j=0;j<26;j++)
 		 if(f[j][x]) f[j][y]=max(f[j][y],f[j][x]+len);
 		 
 		 f[x][y]=max(f[x][y],len);
 	}
 	int ans=0;
 	for(i=0;i<26;i++) ans=max(ans,f[i][i]);
 	cout<<ans<<endl;
 }
 
 signed main(){
 	sov();
 }
 
 
 

 

posted on 2023-02-22 11:52  towboat  阅读(21)  评论(0)    收藏  举报