Codeforces Round #288 (Div. 2)---D. Tanya and Password
来源:互联网 发布:用java编写一个计算器 编辑:程序博客网 时间:2024/05/21 02:19
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
我们把这些三个字符的串看成边,即如果有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;}
- 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
- poj 3252 数位dp (数位统计)
- StageWebView通信
- 正则表达式字面量
- Linux 下安装锐捷,并用vpn进行二次拨号的实现-----浙江理工大学
- 可替代CSS3 transition和transform的jQuery插件
- Codeforces Round #288 (Div. 2)---D. Tanya and Password
- 第一行代码Android-------Activity
- 由基金公司与股民 论外观模式(也叫门面模式)
- Python实现KMP算法
- 图片切割工具类
- C++标准库---序列式容器的移除算法
- 使用libtidy解析html文档的小例子一个
- iOS学习4:UITextView的用法及技巧(ios7下光标bug解决方案)
- 单片机系统常用接口电路、功能模块和外设