文章
99
粉丝
120
获赞
8
访问
110.1k
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
int n;
std::string a[1001];
std::vector<int> e[1001];
int ind[1001];
int rnd[1001];
bool used[1001];
void dfs(int s,string curr, int count) {
if (count == n) {
curr[curr.length() - 1] = ' '; // 把最后一个 '.' 去掉。
std::cout << curr;
exit(0);
}
for (auto i : e[s])
if (!used[i]) {
used[i] = true;
dfs(i, curr + a[i] + '.', count + 1);
used[i] = false;
}
}
int main() {
std::cin >> n;
for (int i = 1; i <= n; ++i) {
std::cin >> a[i];
++ind[a[i][0]];
++rnd[a[i][a[i].length() - 1]];
}
std::sort(a + 1, a + n + 1);
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j)
if (i != j && a[i][a[i].length() - 1] == a[j][0])
e[i].push_back(j);
for (int i = 1; i <= n; ++i)
if (ind[a[i][0]] == rnd[a[i][0]] + 1) {
used[i] = true;
dfs(i, a[i] + '....
登录后发布评论
暂无评论,来抢沙发