跳到正文

生活·游记

十五粉粉福

Blue_young约 3 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 3 分钟

版本

  1. v1当前

欢迎评论告诉我你的想法(黑粉勿入)

目前不能接受的有:别给我想那些歪门邪道的

目前能接受的有:曝字迹…合理即可

爆生日:2012-06-14

报名字缩写:KWY

报马蜂:

C++
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
string a[maxn], ans[maxn], now[maxn];
int book[maxn], len[maxn], flag, n, sum;
map<char, int> s1, s2;
void dfs(int last, int step) {
	if (flag) return;
	if (step == n) {
		flag = 1;
		for (int i = 1; i <= sum; i ++) ans[i] = now[i];
		return;
	}
	for (int i = 1; i <= n; i ++) {
		if (book[i]) continue;
		if (a[last][a[last].length() - 1] == a[i][0]) {
			now[++sum] = a[i], book[i] = 1;
			dfs(i, step + 1);
			book[i] = 0, sum --;
		}
	}
}
int main() {
	cin >> n;
	for (int i = 1; i <= n; i ++) { cin >> a[i]; len[i] = a[i].length(), s1[a[i][0]]++, s2[a[i][len[i] - 1]] ++; }
	int start = 1;
	sort(a + 1, a + 1 + n);
	char s, t;
	for (char c = 'a'; c <= 'z'; c++) if (abs(s1[c] - s2[c]) == 1)
			if (s1[c] - s2[c] == 1) s = c;
			else if (s2[c] - s1[c] == 1) t = c;
	int cnt = s2[t];
	for (int i = 1; i <= n; i++) if (a[i][0] == s && (a[i][len[i] - 1] != t || cnt != 1)) {
			start = i;
			break;
		}
	book[start] = 1, now[++sum] = a[start];
	dfs(start, 1);
	if (!flag) cout << "***\n";
	else for (int i = 1; i <= n; i ++) {
			if (i != n)cout << ans[i] << '.';
			else cout << ans[i];
		}
	cout << endl;
	return 0;
}

爆博客:https://www.cnblogs.com/BLue-young

想看二十粉和二十五粉请进入50635团

原文