Codeforces Round #288 (Div. 2)---D. Tanya and Password

D. Tanya and Password
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

While dad was at work, a little girl Tanya decided to play with dad's password to his secret database. Dad's password is a string consisting of n + 2 characters. She has written all the possible n three-letter continuous substrings of the password on pieces of paper, one for each piece of paper, and threw the password out. Each three-letter substring was written the number of times it occurred in the password. Thus, Tanya ended up with n pieces of paper.

Then Tanya realized that dad will be upset to learn about her game and decided to restore the password or at least any string corresponding to the final set of three-letter strings. You have to help her in this difficult task. We know that dad's password consisted of lowercase and uppercase letters of the Latin alphabet and digits. Uppercase and lowercase letters of the Latin alphabet are considered distinct.

Input

The first line contains integer n (1 ≤ n ≤ 2·105), the number of three-letter substrings Tanya got.

Next n lines contain three letters each, forming the substring of dad's password. Each character in the input is a lowercase or uppercase Latin letter or a digit.

Output

If Tanya made a mistake somewhere during the game and the strings that correspond to the given set of substrings don't exist, print "NO".

If it is possible to restore the string that corresponds to given set of substrings, print "YES", and then print any suitable password option.

Sample test(s)
Input
5
aca
aba
aba
cab
bac
Output
YES
abacaba
Input
4
abc
bCb
cb1
b13
Output
NO
Input
7
aaa
aaa
aaa
aaa
aaa
aaa
aaa
Output
YES
aaaaaaaaa


我们把这些三个字符的串看成边,即如果有abc, 分别把ab, bc看成点,从ab向bc连一条边,那么点的个数最多就是62*62个

这样如果最后这些串能接起来,意味着图中存在欧拉通路,于是我们只要去找到这条路就行,注意先行判断图中是否存在欧拉通路(有向图)


/*************************************************************************
    > File Name: cf288d.cpp
    > Author: ALex
    > Mail: zchao1995@gmail.com 
    > Created Time: 2015年01月28日 星期三 21时20分27秒
 ************************************************************************/

#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

const double pi = acos(-1);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
const int N = 200010;
char str[5];
char st[N << 2];
bool vis[N];
int father[N];
int in[N];
int out[N];
vector <int> edge[N];

int get_N (char c)
{
	if (c >= 'a' && c <= 'z')
	{
		return c - 'a';
	}
	else if (c >= 'A' && c <= 'Z')
	{
		return c - 'A' + 26;
	}
	else if (c >= '0' && c <= '9')
	{
		return c - '0' + 52;
	}
}

char n_c (int x)
{
	if (x >= 0 && x <= 25)
	{
		return 'a' + x;
	}
	else if (x >= 26 && x <= 51)
	{
		return 'A' + x - 26;
	}
	else
	{
		return '0' + x - 52;
	}
}

int ans[N << 1];
int cnt;

int find (int x)
{
	if (father[x] == -1)
	{
		return x;
	}
	return father[x] = find (father[x]);
}

void dfs (int u)
{
	while (!edge[u].empty())
	{
		int v = edge[u].back();
		edge[u].pop_back();
		dfs (v);
		ans[++cnt] = v % 62;
	}
}

void init ()
{
	memset (father, -1, sizeof(father));
	memset (vis, 0, sizeof(vis));
	memset (in, 0, sizeof (in));
	memset (out, 0, sizeof (out));
	cnt = 1;

}
int main ()
{
	int n;
	while (~scanf("%d", &n))
	{
		init ();
		for (int i = 0; i < 62 * 62; ++i)
		{
			edge[i].clear();
		}
		for (int i = 1; i <= n; ++i)
		{
			scanf("%s", str);
			int u = get_N (str[0]) * 62 + get_N (str[1]);
			int v = get_N (str[1]) * 62 + get_N (str[2]);
			vis[u] = 1;
			vis[v] = 1;
			edge[u].push_back(v);
//			printf("%d --> %d\n", u, v);
			++in[v];
			++out[u];
			int fu = find (u);
			int fv = find (v);
			if (fu != fv)
			{
				father[fu] = fv;
			}
		}
		int num = 0;
		for (int i = 0; i < 62 * 62; ++i)
		{
			if (!vis[i])
			{
				continue;
			}
			if (father[i] == -1)
			{
				++num;
			}
		}
		if (num > 1)
		{
			printf("NO\n");
			continue;
		}
		int s = -1;
		int c1 = 0;
		int c2 = 0;
		bool flag = false;
		for (int i = 0; i < 62 * 62; ++i)
		{
			if (!vis[i])
			{
				continue;
			}
			if (out[i] - in[i] == 1)
			{
				++c1;
				s = i;
			}
			else if (out[i] - in[i] == -1)
			{
				++c2;
			}
			else if (out[i] != in[i])
			{
				flag = true;
				break;
			}
		}
//		printf("%d %d\n", c1, c2);
		if (flag || !((c1 == 0 && c2 == 0) || (c1 == 1 && c2 == 1)))
		{
			printf("NO\n");
			continue;
		}
		if (s == -1)
		{
			for (int i = 0; i < 62 * 62; ++i)
			{
				if (vis[i])
				{
					s = i;
					break;
				}
			}
		}
		printf("YES\n");
		dfs (s);
		ans[0] = s / 62;
		ans[1] = s % 62;
		printf("%c%c", n_c(ans[0]), n_c(ans[1]));
		for (int i = cnt; i >= 2; --i)
		{
			printf("%c", n_c(ans[i]));
		}
		printf("\n");
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值