6-10RE,11wa,求调(^~^)
  • 板块P1127 词链
  • 楼主sanbing001
  • 当前回复0
  • 已保存回复0
  • 发布时间2024/11/4 14:02
  • 上次更新2024/11/4 16:53:17
查看原帖
6-10RE,11wa,求调(^~^)
1203087
sanbing001楼主2024/11/4 14:02
#include <bits/stdc++.h>

using namespace std;

int n, m;
vector<int> e[27];
vector<string> str[27];
vector<int> path;
int f[27], ind[27], outd[27], len[27], cnt[27];

void dfs(int u)
{
	while (f[u] < len[u])
	{
		int v = e[u][f[u] ++ ];
		
		dfs(v);
		
		path.push_back(v);
	}
}

void koula()
{
	int x = 0, y = 0;
	for (int i = 1; i < 27; i ++ )
	{
		if (ind[i] + 1 == outd[i]) x = i;
		if (ind[i] != outd[i]) y ++ ;
	}
	
	
	if (!(!y || x && y == 2))
	{
		cout << "***\n";
		return;
	}
	if (!x)
		for (int i = 1; i <= 26; i ++ )
			if (ind[i])
			{
				x = i;
				break;
			}
	dfs(x);
	path.push_back(x);
	
	if (path.size() != n + 1)
	{
		cout << "***\n";
		return;
	}
	
	for (int i = n; i; i -- )
	{
		int u = path[i];
		cout << str[u][cnt[u]];
		if (i > 1) cout << '.';
		cnt[u] ++ ;
	}
}

int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	cin >> n;
	for (int i = 0; i < n; i ++ )
	{
		string s;cin >> s;
		int a = s[0] - 'a' + 1, b = s[s.size() - 1] - 'a' + 1;
		e[a].push_back(b);
		str[a].push_back(s);
		outd[a] ++ ;
		ind[b] ++ ;
		len[a] ++ ;
	}
	
	for (int i = 1; i <= n; i ++ )
		sort(e[i].begin(), e[i].end()),
		sort(str[i].begin(), str[i].end());
	
	koula();
	return 0;
}

2024/11/4 14:02
加载中...