提交时间:2023-10-29 12:30:10
运行 ID: 107996
#include <bits/stdc++.h> using namespace std; const int N = 20+1; int n; string word[N]; int g[N][N]; int used[N]; int ans; void search(string dragon, int last) { ans = max((int)dragon.size(), ans); used[last]++; for (int i = 0; i < n; i++) { if (g[last][i] && used[i] < 2) search(dragon + word[i].substr(g[last][i]),i); } used[last]--; } int main() { char start; cin >> n; for (int i = 0; i < n; i++) cin >> word[i]; cin>> start; for(int i=0;i<n;i++) for (int j = 0; j < n; j++) { string a = word[i], b = word[j]; for (int k = 1; k < min(a.size(), b.size()); k++) { if (a.substr(a.size() - k, k) == b.substr(0, k))//字串 { g[i][j] = k; break; } } } for (int i = 0; i < n; i++) { if (word[i][0] == start) search(word[i], i); } cout << ans << endl; return 0; }