Run ID | 作者 | 问题 | 语言 | 测评结果 | 分数 | 时间 | 内存 | 代码长度 | 提交时间 |
---|---|---|---|---|---|---|---|---|---|
180698 | 黄锦昱 | 单词接龙 | C++ | 解答错误 | 40 | 0 MS | 252 KB | 839 | 2024-08-21 17:22:57 |
#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; }