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.

Examples
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

对于每个字符串,前两个字符为节点,后两个字符为节点,两个节点之间连一条边,最后证明生成的图中是否存在欧拉路即可,并输出路径

#include <bits/stdc++.h>
#define maxn 200005
using namespace std;
typedef long long ll;

char pp[4005][3];
int mp[205][205];
vector<int> v[4005];
int vis[4005], cnt = 0, k1[4005], k2[4005], kk = 0;
char str[4], ans[200010];
void dfs(int j){//记录欧拉路径 
        while(vis[j] < v[j].size()){
        	dfs(v[j][vis[j]++]);
        }
        ans[kk++] = pp[j][1];
}
int find(int b){
	int j = b;
	while(j != vis[j])
	 j = vis[j];
	int ss = b;
	while(vis[ss] != j){
		b = vis[ss];
		vis[ss] = j;
		ss = b;
	}
	return j;
}
int main(){
//	freopen("in.txt", "r", stdin);
	int n, from, to;
	scanf("%d", &n);
	for(int i = 1; i <= 4000; i++)
	 vis[i] = i; 
	for(int i = 0; i < n; i++){
	 scanf("%s", str);
	 if(mp[str[0]][str[1]] == 0){
	 	mp[str[0]][str[1]] = ++cnt;
	 	pp[cnt][0] = str[0];
	 	pp[cnt][1] = str[1];
	 	from = cnt;
	 }
	 else
	  from = mp[str[0]][str[1]];
	 if(mp[str[1]][str[2]] == 0){
	 	mp[str[1]][str[2]] = ++cnt;
	 	pp[cnt][0] = str[1];
	 	pp[cnt][1] = str[2];
	 	to = cnt;
	 }
	 else
	  to = mp[str[1]][str[2]];
	  v[from].push_back(to);
	  k2[from]++;
	  k1[to]++;
	 int b1 = find(from), b2 = find(to);
	 if(b1 != b2)
	  vis[b1] = b2;
    }
    int c = 0;
    for(int i = 1; i <= cnt; i++)
     if(vis[i] == i){
     	c++;
     }
     if(c > 1){
     	puts("NO");
     	return 0;
     }
     int s1 = -1, s2 = -1, f = 0;
     for(int i = 1; i <= cnt; i++){
     	if(abs(k1[i] - k2[i]) > 1){
     		puts("NO");
     		return 0;
     	}
     	if(k1[i] > k2[i]){
     	 s1 = i;
     	 f++;
        }
     	if(k2[i] > k1[i]){
     	 s2 = i;
     	 f++;
        }
     }
     if(f > 2){
     	puts("NO");
     	return 0;
     }
     if(s1 == -1 && s2 == -1){
     	s2 = 1;
     	puts("YES");
     }
     else
     if(s1 != -1 && s2 != -1)
      puts("YES");
    memset(vis, 0, sizeof(vis));
    dfs(s2);
    printf("%c", pp[s2][0]);
    for(int i = kk - 1; i >= 0; i--)
     printf("%c", ans[i]);
    puts("");
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值