提交时间:2024-08-21 17:29:51
运行 ID: 180716
#include<bits/stdc++.h> using namespace std; const int N=25; int n; string word[N]; int g[N][N]; int used[N]; int sum=0; void dfs(string dragon,int last){ sum=max((int)dragon.size(),sum); used[last]++; for(int i=0;i<n;i=i+1){ if(used[i]<2 && g[last][i]){ dfs(dragon+word[i].substr(g[last][i]),i); } } used[last]--; } int main(){ cin>>n; for(int i=0;i<n;i=i+1){ cin>>word[i]; } char c; cin>>c; for(int i=0;i<n;i=i+1){ string a=word[i],b=word[i]; for(int j=0;j<n;j=j+1){ for(int k=1;k<min(a.size(),b.size());k=k+1){ if(a.substr(a.size()-k,k)==b.substr(0,k)){ g[i][j]=k; break; } } } } for(int i=0;i<n;i=i+1){ if(word[i][0]==c){ dfs(word[i],i); } } cout<<sum<<endl; getchar(); getchar(); }