提交时间:2024-08-21 13:08:04
运行 ID: 180429
#include <bits/stdc++.h> using namespace std; const int N = 22; int G[N][N]; string word[N]; int cnt[N]; int n; int res; void dfs(string str, int last) { res = max(res, (int)str.size()); cnt[last]++; for (int i = 0; i < n; i++) { if (G[last][i] && cnt[i] < 2) { dfs(str + word[i].substr(G[last][i]), i); } } cnt[last]--; } int main() { cin >> n; for (int i = 0; i < n; i++) cin >> word[i]; char ch; cin >> ch; 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] == ch) dfs(word[i], i); } cout << res << endl; return 0; }