Codeforces Round #288 (Div. 2) D. Tanya and Password
来源:互联网 发布:厂品商设计软件 编辑:程序博客网 时间:2024/05/21 03:27
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 ofn + 2 characters. She has written all the possiblen 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.
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.
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.
5acaabaabacabbac
YESabacaba
4abcbCbcb1b13
NO
7aaaaaaaaaaaaaaaaaaaaa
YESaaaaaaaaa
哈希一次字符串,然后跑一次欧拉路
#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;}elsebreak;}}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);elseans.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;}elsesum+=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];}}elsecout<<"NO";}
- Codeforces Round #288 (Div. 2)---D. Tanya and Password
- Codeforces Round #288 (Div. 2) D. Tanya and Password
- Codeforces Round #288 (Div. 2) D Tanya and Password
- Codeforces Round #288 (Div. 2)---D. Tanya and Password
- CodeForces 508D(Codeforces Round #288 (Div. 2))Tanya and Password
- Codeforces Round #288 (Div. 2) D.Tanya and Password(欧拉路径)
- Codeforces Round #288 (Div. 2)D.Tanya and Password——欧拉通路
- Codeforces Round #288 (Div. 2) D. Tanya and Password (欧拉通路)
- Codeforces Round #288 (Div. 2) D. Tanya and Password 欧拉回路
- Codeforces Round #288 (Div. 2)-D. Tanya and Password(欧拉路径及其打印)
- codeforces Round#288D Tanya and Password 欧拉通路
- 【欧拉回路】 Codeforces 288 D Tanya and Password
- Codeforces 508D Tanya and Password
- Codeforces 508D Tanya and Password
- Codeforces Round #293 (Div. 2) -- B. Tanya and Postcard
- Codeforces Round #293 (Div. 2) B. Tanya and Postcard
- Codeforces Round #324 (Div. 2) B. Kolya and Tanya
- Codeforces Round #324 (Div. 2) B. Kolya and Tanya
- 黑马程序员 -- IOS C语言 基础运算01
- Programing Booking List
- poj2376区间贪心
- 2014年秋 求职总结
- SDK manager 中 Done loading packages 问题
- Codeforces Round #288 (Div. 2) D. Tanya and Password
- MongoDB入门(三)-Java操作MongoDB
- 小寒假第二天总结
- CF 161D Distance in Tree 树形DP
- 调试struts2两种方法之配置插件和使用Debugging Interceptor
- MySQL安装之后没有MySQL数据库的原因
- Android学习笔记 - 杂
- golang todo实例
- hdu 5014 亦或的性质