CodeForces 508D Tanya and Password欧拉路径
来源:互联网 发布:ios 数据共享 编辑:程序博客网 时间:2024/05/21 05:39
那可是D题啊@。@ 做不上我心安了
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=88120#problem/B
Description
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 Input
5acaabaabacabbac
YESabacaba
4abcbCbcb1b13
NO
7aaaaaaaaaaaaaaaaaaaaa
YESaaaaaaaaa
欧拉回路,百闻不如一见==还是得先判断存在与否,根据入度出度,这个代码处理的好巧妙
最最重要的是dfs写回路!!
#include <iostream>#include <cstdio>#include <cstring>using namespace std;const int N1=2e5+5,N2=4e3; // (26*2+10)^2<4000int f[N2],in[N2],out[N2],path[N1],e[N2][N2];bool has[N2];int len,st,n;struct node{ int u,v; char name[5];}snode[N1];int abs(int x){ return x>0?x:(-x);}void init(){ for(int i=0;i<N2;i++) f[i]=i; memset(in,0,sizeof(in)); memset(out,0,sizeof(out)); memset(path,0,sizeof(path)); memset(e,0,sizeof(e)); memset(has,0,sizeof(has));}int find(int x){ if(x==f[x]) return x; return f[x]=find(f[x]);}int ctoi(char ch){ if(ch<='9'&&ch>='0') return ch-'0'; if(ch>='A'&&ch<='Z') return ch-'A'+10; return ch-'a'+36;}char itoc(int x){ if(x<=9&&x>=0) return x+'0'; if(x<=35&&x>=10) return x+'A'-10; return x+'a'-36;}bool Exist(){ int t=-1,sum=0,temp; for(int i=0;i<N2;i++){ if(has[i]){ if(t==-1) t=find(i); else if(find(i)!=t) return 0; } } for(int i=0;i<N2;i++){ if(has[i]){ temp=i; if(in[i]!=out[i]){ sum++; if(abs(in[i]-out[i])>1) return 0; if(out[i]>in[i]) st=i; //链的端点 } } } if(sum>2) return 0; if(sum==0) st=temp; // 环的一节点 return 1;}void dfs(int q){ for(int i=0;i<N2;i++){ while(e[q][i]){ e[q][i]--; dfs(i); path[len++]=i; } }}int main(){ //freopen("cin.txt","r",stdin); int n; while(cin>>n){ init(); for(int i=0;i<n;i++){ scanf("%s",snode[i].name); snode[i].u=ctoi(snode[i].name[0])*62+ctoi(snode[i].name[1]); snode[i].v=ctoi(snode[i].name[1])*62+ctoi(snode[i].name[2]); int a=snode[i].u,b=snode[i].v; has[a]=has[b]=1; f[find(a)]=f[find(b)]; out[a]++; in[b]++; e[a][b]++; } if(!Exist()) puts("NO"); else { len=0; dfs(st); path[len++]=st; puts("YES"); //cout<<path[len-1]<<endl; printf("%c%c",itoc(path[len-1]/62),itoc(path[len-1]%62)); for(int i=len-2;i>=0;i--){ printf("%c",itoc(path[i]%62)); } puts(""); } } return 0;}
- Tanya and Password - CodeForces 508 D 欧拉路径
- CodeForces 508D Tanya and Password(欧拉路径)
- CodeForces 508D Tanya and Password欧拉路径
- Codeforces 508D Tanya and Password 欧拉通路Euler
- Codeforces 508D - Tanya and Password (欧拉道路)
- codeforces - 508D - 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
- codeforecs--D. Tanya and Password(输出欧拉路径)
- CF 508D(Tanya and Password-欧拉路径,弗罗莱算法)
- 【CF 508D】 Tanya and Password (判欧拉路+输出欧拉路径)
- [CF508D] Tanya and Password && 欧拉路径
- Codeforces 508D Tanya and Password
- Codeforces 508D Tanya and Password
- Codeforces Round #288 (Div. 2)D.Tanya and Password——欧拉通路
- Codeforces Round #288 (Div. 2) D. Tanya and Password (欧拉通路)
- Android下JSON解析的方式
- mac强制关掉当前激活的界面
- ubuntu上安装mysql
- 基于Android中Looper , Handler , Message的线程池,轻松解决Sqlite数据库的线程安全问题
- T-SQL with关键字(转载)
- CodeForces 508D Tanya and Password欧拉路径
- 《开源框架那点事儿32》:挑战编程极限的问题
- Android 快速开发系列 打造万能的ListView GridView 适配器
- 二叉树利用前序遍历+中序遍历---->后序遍历(把整个树建立起来)
- 利用oracle的游标为员工涨工资,从最低工资涨起每人涨10%,但工资总额不能超过5万元
- prctl()函数应用
- 欢迎使用CSDN-markdown编辑器
- IOS-tableview编辑
- 输入框的常用代理方法