#include<iostream> #include<cstring> #include<cmath> using namespace std; int n,length=0,vis[1000]={0};string str[1000]; inline int check(string a,string b) { int p=min(a.length(),b.length()); for(int i=1;a.length()==1? i<=p:i<p;i++) { bool flag=true; for(int j=0;j<i;j++) { if(a[a.length()-i+j]!=b[j]) { flag=false; break; } } if(flag==true) return i; } return 0; } void dfs(string s,int length_now) { length=max(length,length_now); for(int i=1;i<=n;i++) { if(vis[i]>1) continue; else { int add=check(s,str[i]); if(add!=0) { vis[i]++; dfs(str[i],length_now+str[i].length()-add); vis[i]--; } } } } int main() { cin>>n; for(int i=1;i<=n;i++) cin>>str[i]; cin>>str[n+1]; dfs(str[n+1],1); cout<<length<<endl; return 0; }
标签:单词,int,add,P1859,vis,length,接龙,str,include From: https://www.cnblogs.com/lhf123/p/17430825.html