现在的位置: 首页 > 综合 > 正文

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

2019年11月09日 ⁄ 综合 ⁄ 共 3139字 ⁄ 字号 评论关闭
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

哈希一次字符串,然后跑一次欧拉路

#include<map>
#include<string>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
#include<bitset>
#include<climits>
#include<list>
#include<iomanip>
#include<stack>
#include<set>
using namespace std;
map<string,int>toint;
vector<string>tostring;
vector<int>box;
vector<int>edge[62*62*62+10];
void seek(int from)
{
	while(1)
	{	
		box.push_back(from);
		if(edge[from].size())
		{
			int to=edge[from].back();
			edge[from].pop_back();
			from=to;
		}
		else
			break;
	}
}
vector<int>ans;
void work(int s)
{
	box.push_back(s);
	while(box.size())
	{
		int from=box.back();
		box.pop_back();
		if(edge[from].size())
			seek(from);
		else
			ans.push_back(from);
	}
}
int father[62*62*62+10];
int run(int from)
{
	return father[from]==from?from:father[from]=run(father[from]);
}
bool isok()
{
	int sum=0;
	for(int i=0;i<toint.size()&&sum<2;i++)
		if(father[i]==i)
			sum++;
	return sum<=1;
}
vector<string>in;
int ind[62*62*62+10],out[62*62*62+10];
int main()
{
	int n;
	cin>>n;
	while(n--)
	{
		string s;
		cin>>s;
		in.push_back(s.substr(0,2));
		in.push_back(s.substr(1,2));
	}
	for(int i=0;i<in.size();i++)
		toint[in[i]];
	map<string,int>::iterator it=toint.begin();
	for(int i=0;it!=toint.end();it++,i++)
	{
		tostring.push_back(it->first);
		it->second=i;
	}
	for(int i=0;i<toint.size();i++)
		father[i]=i;
	for(int i=0;i<in.size();i+=2)
		father[run(toint[in[i]])]=run(toint[in[i+1]]);
	if(isok())
	{
		for(int i=0;i<in.size();i+=2)
		{
			out[toint[in[i]]]++;
			ind[toint[in[i+1]]]++;
			edge[toint[in[i]]].push_back(toint[in[i+1]]);
		}
		int s=0,sum=0;
		for(int i=0;i<toint.size()&&sum<3;i++)
		{
			if(ind[i]!=out[i])
			{
				if(abs(ind[i]-out[i])==1)
				{
					sum++;
					if(ind[i]<out[i])
						s=i;
				}
				else
					sum+=3;
			}
		}
		if(sum>2)
			cout<<"NO";
		else
		{
			cout<<"YES"<<endl;
			work(s);
			cout<<tostring[ans.back()];
			for(int i=ans.size()-2;i>-1;i--)
				cout<<tostring[ans[i]][1];
		}
	}
	else
		cout<<"NO";
}
【上篇】
【下篇】

抱歉!评论已关闭.