如题。
#2: Wrong Answer.wrong answer On line 1 column 1, read *, expected w.
#3: Wrong Answer.wrong answer On line 1 column 11, read s, expected k.
#6: Wrong Answer.wrong answer On line 1 column 2, read e, expected b.
#7 ~ #10: RE
#include <bits/stdc++.h>
typedef long long ll;
const int Al = 30;
struct node {
int to; std::string str;
bool operator < (const node &cmp) const {
if (str.length() < cmp.str.length()) return str.length() < cmp.str.length();
for (int i = 0; i < str.length(); ++ i) if (str[i] < cmp.str[i]) return 1;
return 0;
}
}; std::vector <node> G[Al]; // to save the graph
int n;
int in[Al], out[Al], anc[Al];
int find(int u) {
if (anc[u] == u) return u;
else return anc[u] = find(anc[u]);
}
void merge(int u, int v) {
anc[find(u)] = find(v);
return void();
}
int Jud() {
// if it's not connected
for (int i = 1; i <= 26; ++ i) anc[i] = i;
int flag = 0;
for (int i = 1; i <= 26; ++ i)
for (int j = 0; j < G[i].size(); ++ j) if (find(i) != find(G[i][j].to)) merge(i, G[i][j].to), flag = i;
// for (int i = 1; i <= 26; ++ i) std::cout << find(i) << " ";
for (int i = 1; i <= 26; ++ i) if ((in[i] || out[i]) && find(i) != find(flag)) return -1;
// std::cout << "here\n";
// if it's not an Euler Graph
int cnt = 0;
for (int i = 1; i <= 26; ++ i) cnt += (in[i] == out[i]);
if (cnt != 26 && cnt != 24) return -1;
if (cnt == 26) {
int fl = 0;
for (int i = 1; i <= 26; ++ i) if (in[i] || out[i]) return i;
}
if (cnt == 24) {
int c1 = 0, c2 = 0;
for (int i = 1; i <= 26; ++ i) c1 += (in[i] - out[i] == 1), c2 += (out[i] - in[i] == 1);
if (! (c1 == 1 && c2 == 1)) return -1;
// std::cout << c1 << " " << c2 << "\n";
for (int i = 1; i <= 26; ++ i) if (out[i] - in[i] == 1) return i;
}
}
int del[Al];
std::vector <std::string> ans;
void dfs(int u) {
for (; del[u] < G[u].size(); ) {
int v = G[u][del[u]].to;
std::string str = G[u][del[u]].str;
++ del[u];
dfs(v);
ans.push_back(str);
}
return void();
}
int main() {
// freopen(".in", "r", stdin);
// freopen(".out", "w", stdout);
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cout.tie(nullptr);
std::cin >> n;
for (int i = 1; i <= n; ++ i) {
std::string str = "";
std::cin >> str;
int u = str[0] - 'a' + 1, v = str[str.length() - 1] - 'a' + 1;
G[u].push_back((node) {v, str});
++ in[v]; ++ out[u];
}
// for (int i = 1; i <= 26; ++ i) std::cout << in[i] << " " << out[i] << "\n";
int s = Jud(); // start from s
if (s == -1) return std::cout << "***\n", 0;
// std::cout << s << "\n";
for (int i = 1; i <= 26; ++ i) std::sort(G[i].begin(), G[i].end());
dfs(s);
for (int i = ans.size() - 1; i; -- i) std::cout << ans[i] << ".";
std::cout << ans[0] << "\n";
return 0;
}