题目链接

给你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 欧拉通路的更多相关文章

  1. CodeForces - 508D Tanya and Password(欧拉通路)

    Description While dad was at work, a little girl Tanya decided to play with dad characters. She has ...

  2. ACM/ICPC 之 DFS求解欧拉通路路径(POJ2337)

    判断是欧拉通路后,DFS简单剪枝求解字典序最小的欧拉通路路径 //Time:16Ms Memory:228K #include<iostream> #include<cstring& ...

  3. POJ 1300 欧拉通路&欧拉回路

    系统的学习一遍图论!从这篇博客开始! 先介绍一些概念. 无向图: G为连通的无向图,称经过G的每条边一次并且仅一次的路径为欧拉通路. 如果欧拉通路是回路(起点和终点相同),则称此回路为欧拉回路. 具有 ...

  4. poj 2513 连接火柴 字典树+欧拉通路 好题

    Colored Sticks Time Limit: 5000MS   Memory Limit: 128000K Total Submissions: 27134   Accepted: 7186 ...

  5. poj2513- Colored Sticks 字典树+欧拉通路判断

    题目链接:http://poj.org/problem?id=2513 思路很容易想到就是判断欧拉通路 预处理时用字典树将每个单词和数字对应即可 刚开始在并查集处理的时候出错了 代码: #includ ...

  6. hdu1116有向图判断欧拉通路判断

    Play on Words Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) T ...

  7. Colored Sticks POJ - 2513 并查集+欧拉通路+字典树hash

    题意:给出很多很多很多很多个棒子 左右各有颜色(给出的是单词) 相同颜色的可以接在一起,问是否存在一种 方法可以使得所以棒子连在一起 思路:就是一个判欧拉通路的题目,欧拉通路存在:没奇度顶点   或者 ...

  8. 欧拉回路&欧拉通路判断

    欧拉回路:图G,若存在一条路,经过G中每条边有且仅有一次,称这条路为欧拉路,如果存在一条回路经过G每条边有且仅有一次, 称这条回路为欧拉回路.具有欧拉回路的图成为欧拉图. 判断欧拉通路是否存在的方法 ...

  9. POJ2513Colored Sticks(欧拉通路)(字典树)(并查集)

                                                             Colored Sticks Time Limit: 5000MS   Memory ...

随机推荐

  1. 利用虚拟光驱实现 将WINDOWS文件供虚拟机中的UBUNTU共享

    此方法只能实现(至少目前我发现只能这样)将文件传递给虚拟机中的ubuntu 中,供ubuntu系统阅读,拷贝等,但不能将ubuntu中的数据传递给windows. 即:每次更新windows的数据到u ...

  2. IIS7.5(IIS7)配置伪静态urlrewrite

    找了好久,终于找到了.已经测试通过,收藏. 转载自:http://jingyan.baidu.com/article/67508eb4ff92c69cca1ce49a.html 首先新建一个应用程序池 ...

  3. Windows下命令行下启动ORACLE服务

    检查监听器状态:C:\>lsnrctl statusLSNRCTL for 32-bit Windows: Version 9.2.0.1.0 - Production on 30-6月 -20 ...

  4. 转场动画1-Push 动画

    先上效果图: 这篇文章完全是为造轮子制作:原作者是码农界的吴彦祖 作者视频下载地址 好的,我梳理一下思路: 理清思路 ||转场动画可以理解为一个对象,在这个对象里封装了一个动画.具体的我们跟着代码走 ...

  5. 经典SQL语句集锦

      下列语句部分是MsSql语句,不可以在access中使用. SQL分类: DDL—数据定义语言(CREATE,ALTER,DROP,DECLARE) DML—数据操纵语言(SELECT,DELET ...

  6. Labview学习之远程控制VI

    Labview学习之远程控制VI        从LabVIEW 6.1开始,LabVIEW集成了Remote Panels技术,允许用户直接在客户端计算机上打开并操作位于服务器端计算机上的VI的前面 ...

  7. MVC定义路由

    标准路由配置 routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( name: "Defa ...

  8. 解决ListView 和ScroolView 共存 listItem.measure(0, 0) 空指针

    在网上找到ListView 和ScroolView 共存的方法无非是给他每个listview 重新增加高度,但是android 的设计者始终认为这并不是一种好的实现方法.但是有的时候有必须要用这种蛋疼 ...

  9. 练习:一只豆瓣电影TOP250的爬虫

    练习:一只豆瓣电影TOP250爬虫 练习:一只豆瓣电影TOP250爬虫 ①创建project ②编辑items.py import scrapyclass DoubanmovieItem(scrapy ...

  10. 1501 二叉树最大宽度和高度 (BFS+树的遍历)

    题目:http://www.wikioi.com/problem/1501/ 给你一颗二叉树,求该数的宽和高, 首先求出树的高,直接进行二叉树遍历,能够得到二叉树的高 然后是得到宽,本人采用的是一层一 ...