codeforces 508D . Tanya and Password 欧拉通路
给你n个长度为3的子串, 这些子串是由一个长度为n+2的串分割得来的, 求原串, 如果给出的不合法, 输出-1。
一个欧拉通路的题, 将子串的前两个字符和后两个字符看成一个点, 比如acb, 就是ac->cb。 然后建图。
#include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <stack>
#include <string>
#include <queue>
using namespace std;
#define pb(x) push_back(x)
#define ll long long
#define mk(x, y) make_pair(x, y)
#define lson l, m, rt<<1
#define mem(a) memset(a, 0, sizeof(a))
#define rson m+1, r, rt<<1|1
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define rep(i, n, a) for(int i = a; i<n; i++)
#define fi first
#define se second
typedef pair<int, int> pll;
const double PI = acos(-1.0);
const double eps = 1e-;
const int mod = 1e9+;
const int inf = ;
const int dir[][] = { {-, }, {, }, {, -}, {, } };
map <string, int> ma;
string a[];
int cnt, inde[], outde[], vis[*], num, ans[*], ecnt[];
vector <int> v[];
void dfs(int u) {
while(ecnt[u]<v[u].size()) {
dfs(v[u][ecnt[u]++]);
}
ans[num++] = u%;
}
int main()
{
ios::sync_with_stdio();
string s, tmp;
int n, pos1, pos2;
cin>>n;
int cnt = , start;
for(int i = ; i<n; i++) {
cin>>s;
int u = *s[]+s[];
int to = *s[]+s[];
v[u].pb(to);
inde[to]++;
outde[u]++;
start = u;
}
pos1 = -, pos2 = -;
int flag = ;
for(int i = ; i<; i++) {
if(outde[i]!=inde[i]) {
if(outde[i]==inde[i]+) {
if(pos1==-) {
pos1 = i;
} else {
flag = ;
}
} else {
if(pos2==-)
pos2=i;
else
flag = ;
}
}
}
if(flag||(pos1==-&&pos2!=-||pos2==-&&pos1!=-)) {
cout<<"NO"<<endl;
return ;
}
num = ;
if(pos1 == -)
pos1 = start;
dfs(pos1);
if(num != n+) {
puts("NO");
return ;
}
s = "", tmp = "";
tmp = (char)(pos1/);
for(int i = num-; i>=; i--) {
tmp += char(ans[i]);
}
cout<<"YES"<<endl;
cout<<tmp;
return ;
}
codeforces 508D . Tanya and Password 欧拉通路的更多相关文章
- CodeForces - 508D Tanya and Password(欧拉通路)
Description While dad was at work, a little girl Tanya decided to play with dad characters. She has ...
- ACM/ICPC 之 DFS求解欧拉通路路径(POJ2337)
判断是欧拉通路后,DFS简单剪枝求解字典序最小的欧拉通路路径 //Time:16Ms Memory:228K #include<iostream> #include<cstring& ...
- POJ 1300 欧拉通路&欧拉回路
系统的学习一遍图论!从这篇博客开始! 先介绍一些概念. 无向图: G为连通的无向图,称经过G的每条边一次并且仅一次的路径为欧拉通路. 如果欧拉通路是回路(起点和终点相同),则称此回路为欧拉回路. 具有 ...
- poj 2513 连接火柴 字典树+欧拉通路 好题
Colored Sticks Time Limit: 5000MS Memory Limit: 128000K Total Submissions: 27134 Accepted: 7186 ...
- poj2513- Colored Sticks 字典树+欧拉通路判断
题目链接:http://poj.org/problem?id=2513 思路很容易想到就是判断欧拉通路 预处理时用字典树将每个单词和数字对应即可 刚开始在并查集处理的时候出错了 代码: #includ ...
- hdu1116有向图判断欧拉通路判断
Play on Words Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...
- Colored Sticks POJ - 2513 并查集+欧拉通路+字典树hash
题意:给出很多很多很多很多个棒子 左右各有颜色(给出的是单词) 相同颜色的可以接在一起,问是否存在一种 方法可以使得所以棒子连在一起 思路:就是一个判欧拉通路的题目,欧拉通路存在:没奇度顶点 或者 ...
- 欧拉回路&欧拉通路判断
欧拉回路:图G,若存在一条路,经过G中每条边有且仅有一次,称这条路为欧拉路,如果存在一条回路经过G每条边有且仅有一次, 称这条回路为欧拉回路.具有欧拉回路的图成为欧拉图. 判断欧拉通路是否存在的方法 ...
- POJ2513Colored Sticks(欧拉通路)(字典树)(并查集)
Colored Sticks Time Limit: 5000MS Memory ...
随机推荐
- openvswitch安装、基本操作
一.安装,配置 //下载源码.编译.安装: #wget http://openvswitch.org/releases/openvswitch-2.3.0.tar.gz #tar -zxvf open ...
- java获取当前路径的几种方法
1.利用System.getProperty()函数获取当前路径: System.out.println(System.getProperty("user.dir"));//use ...
- eclipse使用技巧---使用正则表达式查找替换
1,Eclipse ctrl+f 打开查找框2,选中 Regular expressions (正则表达式) 去掉/* */(eclipse) /\*(.|[\r\n])*?\*/去掉/ ...
- android UI-EditText的长度监听慎用TextWatcher
在用户昵称的输入时,限定8个字符,本意是在输入超过8个时候,页面toast一个提示,就是下面的TextWatcher的监听,在afterTextChanged中处理. 原bug:huawei MT2- ...
- Java环境的配置
JAVA环境: 1.打开我的电脑--属性--高级--环境变量 2.将相应的JDK环境下载到本机,将路径保存到无中文路径中,并将路径复制下来. 3.在环境变量--系统变量,中新建 变量名:JAVA_HO ...
- FAT下的winhex数据恢复
一·我在自己的U盘中建一个一个文件abc.word,然后删除 二·用winhex打开需要恢复的磁盘 我这是U盘 I:按确定打开它 三.来到它的根目录下 四·查找文件名,找到文件目录项 文件起始簇号:5 ...
- BZOJ 3207: 花神的嘲讽计划Ⅰ( hash + 可持久化线段树 )
O(NK)暴力搞出所有子串的哈希值, 然后就对哈希值离散化建权值线段树, 就是主席树的经典做法了.总时间复杂度O(NK+(N+Q)logN) ----------------------------- ...
- input 属性
1.readonly="readonly" 用来表示 input只能读写.
- Mybatis使用存储过程(MySql)
推荐文章:http://www.iteye.com/topic/1132302 http://yhjhappy234.blog.163.com/blog/static/3163283220124557 ...
- linux c in common use function reference manual
End User License Agreement guarantees or warranties,大战前得磨刀!!!!! Tips:C Funcs Chk header Modules!