玄学错误(
  • 板块P1127 词链
  • 楼主cjWYZtql
  • 当前回复1
  • 已保存回复1
  • 发布时间2023/5/25 21:03
  • 上次更新2023/10/23 14:46:36
查看原帖
玄学错误(
335771
cjWYZtql楼主2023/5/25 21:03
#include <bits/stdc++.h>

#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define ll long long
#define ul unsigned long long

using namespace std;

struct Edge {
	int to, nxt, vis;
	string dis;
}e[1919810];

int cnt;
int hd[1919810];
int n;
int ind[1919810], outd[1919810];
string str[1145];
int ans[1919810];

void add_edge(int u, int v, string d) {
	++cnt;
	e[cnt].to = v;
	e[cnt].dis = d;
	e[cnt].nxt = hd[u];
	hd[u] = cnt;
}

int reint(char c) {
	return c - 'a' + 1;
}

bool cmp(string x, string y) {
	return x > y;
}

int Dfs(int p, int step) {
	if (step > n) {
		for (int i = 1; i < n; ++i) cout << e[ans[i]].dis << '.';
		cout << e[ans[n]].dis;
		exit(0);
	}
	for (int i = hd[p]; i; i = e[i].nxt) {
		if (e[i].vis == 1) continue;
		e[i].vis = 1;
		ans[step] = i;
		Dfs(e[i].to, step + 1);
		e[i].vis = 0;
	}
}

int main() {
	IOS
	cin >> n;
	int minn = 0x3f3f3f3f;
	for (int i = 1; i <= n; ++i) cin >> str[i];
	sort(str + 1, str + n + 1, cmp);
	for (int i = 1; i <= n; ++i) {
		int u, v;
		u = reint(str[i][0]);
		v = reint(str[i][str[i].size() - 1]);
		add_edge(u, v, str[i]);
		++outd[u];
		++ind[v];
		minn = min(minn, u);
	}
	int tot = 0;
	for (int i = 1; i <= 26; ++i) {
		if (abs(outd[i] - ind[i]) > 1) {
			cout << "***" << '\n';
			return 0;
		} else if (abs(outd[i] - ind[i] == 1)) ++tot;
	}
	if (tot > 2) {
		cout << "***";
		return 0;
	}
	if (tot == 2) {
		for (int i = 1; i <= 26; ++i) {
			if (abs(outd[i] - ind[i]) == 1) Dfs(i, 1);
		}
	} else Dfs(minn, 1);
}

不开 O2 64pts,开了 27pts

2023/5/25 21:03
加载中...