提交时间:2024-08-21 17:22:57

运行 ID: 180698

#include <bits/stdc++.h> using namespace std; const int N = 30; int n; string word[N]; int g[N][N]; int used[N]; int sum = 0; void dfs(string dragon, int last) { sum = max((int)dragon.size(), sum); ++used[last]; for (int i = 0; i < n; ++i) { if (used[i] < 2 && g[last][i]) { dfs(dragon + word[i].substr(g[last][i]), i); } } ++used[last]; } int main() { cin >> n; for (int i = 0; i < n; ++i) { cin >> word[i]; } for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { for (int k = 1; k < min(word[i].size(), word[j].size()); ++k) { if(word[i].substr(word[i].size() - k, word[i].size()) == word[j].substr(0, k)) { g[i][j] = k; break; } } } } char c; cin >> c; for (int i = 0; i < n; ++i) if (word[i][0] == c){ dfs(word[i], i); } cout << sum; }