1117 单词接龙
作者:
jy9
,
2024-08-16 14:17:05
,
所有人可见
,
阅读 1
#include <iostream>
using namespace std;
const int N = 30;
string mp[N];
int st[N];
string tmp;
int n,ans;
void dfs(string target, int pos){
st[pos] ++;
int len = target.size();
ans = max(ans, len);
for (int i = 1; i <= n; i ++ ){
for (int j = len - 1, k = 1; j > 0 && k < mp[i].size(); j --, k ++ ){
if(st[i] < 2 && target.substr(j) == mp[i].substr(0, k)){
string t = target.substr(0, len - k) + mp[i];
dfs(t, i);
}
}
}
st[pos] --;
}
int main(){
cin >> n;
for (int i = 1; i <= n; i ++ )cin >> mp[i];
string start;
cin >> start;
dfs(" " + start, n+1);
cout << ans - 1<< endl;
return 0;
}