Run ID | 作者 | 问题 | 语言 | 测评结果 | 分数 | 时间 | 内存 | 代码长度 | 提交时间 |
---|---|---|---|---|---|---|---|---|---|
180429 | C班-范浩宇 | 单词接龙 | C++ | 通过 | 100 | 0 MS | 252 KB | 1087 | 2024-08-21 13:08:04 |
#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; }