显示代码纯文本
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<string>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<climits>
using namespace std;
inline int qread(){
int ret=0;char ch=getchar();
while(ch<'0'||ch>'9') ch=getchar();
while(ch>='0'&&ch<='9') ret=ret*10+ch-'0',ch=getchar();
return ret;
}
int len[22],dis[22];
string s[22];
int a[22][22];
int n,maxx=0;
int sum[22];
void dfs(int x,int ans){
// cout<<x<<' '<<ans<<endl;
bool flag=0;
for(int i=1;i<=n;++i){
if(a[x][i]>0&&sum[i]<2){
sum[i]++;
dfs(i,ans+a[x][i]);
sum[i]--;
flag=1;
}
}
if(!flag&&ans+a[x][n+1]>maxx){
maxx=ans+a[x][n+1];
return ;
}
}
int main(){
freopen("dcjl.in","r",stdin);
freopen("dcjl.out","w",stdout);
n=qread();
for(int i=1;i<=n;++i){
cin>>s[i];
len[i]=s[i].size();
a[i][n+1]=len[i];
}
for(int i=1;i<=n;++i)
for(int j=1;j<=n;++j){
bool flag=0;
int l=len[i]<len[j]?len[i]:len[j];
for(int kk=1;kk<l;++kk)
for(int k=0;k<kk;++k){
if(s[j][k]!=s[i][len[i]-kk+k]) break;
// cout<<i<<' '<<j<<' '<<kk<<' '<<k<<' ';
// cout<<s[j][k]<<' '<<s[i][len[i]-kk+k]<<endl;
if(kk-k==1){
a[i][j]=len[i]-kk;
flag=1;
}
if(flag) break;
}
}
string c;cin>>c;
for(int i=1;i<=n;++i)
if(s[i][0]==c[0]){
sum[i]++;
dfs(i,0);
sum[i]--;
}
cout<<maxx;
return 0;
}