Codeforces 508D Tanya and Password 欧拉通路Euler
来源:互联网 发布:网件的访客网络 编辑:程序博客网 时间:2024/05/22 08:04
题目链接:点击打开链接
题意:给定n个长度为3各不相同的字符串,猜一个n+2位的密码。
这个密码包含上面的所有字符串
思路:
把 abc 拆成ab -> bc
则得到一个图,目标就是走遍所有 ab->bc 这样的边,也就是找一条欧拉通路。
#include <cstdio>#include <algorithm>#include <string.h>#include <queue>#include <cstring>#include <cmath>#include <iostream>#include <vector>using namespace std;typedef pair<int, int> pii;typedef long long ll;vector<pii>G[500000];int deg[4000];int ans[500000], top;int v[500000];int cur[4000];void eur(int s, int p){int f;while (cur[s]<G[s].size())if (v[G[s][f = cur[s]++].second] == 0){v[G[s][f].second] = 1;eur(G[s][f].first, G[s][f].second);} ans[top++] = p;}char c[200005][5];int n;int ch(char c){if ('0' <= c && c <= '9')return c - '0';if ('a' <= c && c <= 'z')return c - 'a' + 10;return c - 'A' + 36;}int getx(int id){return ch(c[id][0]) * 62 + ch(c[id][1]);}int gety(int id){return ch(c[id][1]) * 62 + ch(c[id][2]);}bool ok(){int x = 0, y = 0, pos = getx(0);for (int i = 0; i < 4000; i++){if (deg[i]>1 || deg[i] < -1)return false;if (deg[i] == 1) x++, pos = i;if (deg[i] == -1)y++;}if (x != y || x>1)return false;eur(pos, -1); top--;return top >= n;}void input(){memset(deg, 0, sizeof deg);memset(cur, 0, sizeof cur);memset(v, 0, sizeof v);scanf("%d", &n);for (int i = 0; i < n; i++){scanf("%s", c[i]);int x = getx(i), y = gety(i);G[x].push_back(pii(y, i));deg[x]++; deg[y]--;}}int main(){input();if (ok()){puts("YES");printf("%s", c[ans[top - 1]]);for (int i = top - 2; i>-1; i--)printf("%s", c[ans[i]] + 2);puts("");}else puts("NO");return 0;}
0 0
- Codeforces 508D Tanya and Password 欧拉通路Euler
- codeforces - 508D - Tanya and Password(欧拉通路)
- codeforces Round#288D Tanya and Password 欧拉通路
- Codeforces Round #288 (Div. 2)D.Tanya and Password——欧拉通路
- Codeforces Round #288 (Div. 2) D. Tanya and Password (欧拉通路)
- codeforces 508D Tanya and Password (有向图欧拉通路)
- Codeforces 508D - Tanya and Password (欧拉道路)
- Tanya and Password - CodeForces 508 D 欧拉路径
- CodeForces 508D Tanya and Password(欧拉路径)
- CodeForces 508D Tanya and Password欧拉路径
- 【欧拉回路】 Codeforces 288 D 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 欧拉回路
- Codeforces Round #288 (Div. 2)-D. Tanya and Password(欧拉路径及其打印)
- codeforecs--D. Tanya and Password(输出欧拉路径)
- 第二十六次codeforces竞技结束 #286 Div 2
- Linux下解压命令大全 解压缩 tar bz2 zip tar.gz gz
- over(Partition by...) ORACLE函数
- ubuntu下使用pre-trained模型测试caffe,找不到caffe 和 protobuf的错误
- Android 如何使用GPU硬件加速
- Codeforces 508D Tanya and Password 欧拉通路Euler
- windows server2003与2008的区别总结
- Linux学习笔记
- Codeforces Beta Round #63 (Div. 2)
- HDU 1213 How Many Tables(kuangbin带你飞 专题五:并差集)
- Q9.10 To build the tallest stack
- ios 页面传值
- poj1845
- 检测 Linux 内存使用情况的 free 命令的10个例子