Codeforces 508D Tanya and Password 欧拉通路Euler

题目链接:点击打开链接

题意:给定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;
}
时间: 2024-08-10 14:17:53

Codeforces 508D Tanya and Password 欧拉通路Euler的相关文章

codeforces 508D . Tanya and Password 欧拉通路

题目链接 给你n个长度为3的子串, 这些子串是由一个长度为n+2的串分割得来的, 求原串, 如果给出的不合法, 输出-1. 一个欧拉通路的题, 将子串的前两个字符和后两个字符看成一个点, 比如acb, 就是ac->cb. 然后建图. 1 #include <iostream> 2 #include <vector> 3 #include <cstdio> 4 #include <cstring> 5 #include <algorithm>

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

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

Codeforces 508D Tanya and Password

题意: n(10^5)个串每个串3个字符  两个串abc.xyz能拼在一起前提是b=x&&c=y  它们能拼成ab(x)c(y)z  求n个串品在一起的串 思路: 将串abc变成ab->bc的一条边  则原题变成了有向图的欧拉路径问题 有向图欧拉路径算法就是遍历  因为欧拉路径其实就是"每条边走一遍" 代码: #include<cstdio> #include<iostream> #include<cstring> #inclu

FZU 2112 并查集、欧拉通路

原题:http://acm.fzu.edu.cn/problem.php?pid=2112 首先是,票上没有提到的点是不需要去的. 然后我们先考虑这个图有几个联通分量,我们可以用一个并查集来维护,假设有n个联通分量,我们就需要n-1条边把他们连起来. 最后对于每个联通分量来说,我们要使它能一次走完,就是要求他是否满足欧拉通路,也就是这个联通分量中至多有2个度为奇数的点,每多出2个度为奇数的点,就多需要一条边(因为单个连通分量的所有点的度数之和为偶数,所以不可能存在奇数个奇数度数的点). 1 #i

POJ--1300--Door Man【推断无向图欧拉通路】

链接:http://poj.org/problem?id=1300 题意:有n个房间.每一个房间有若干个门和别的房间相连.管家从m房间開始走.要回到自己的住处(0),问是否有一条路能够走遍全部的门而且没有反复的路. 无向图欧拉通路充要条件:G为连通图,而且G仅有两个奇度结点(度数为奇数的顶点)或者无奇度结点. 无向图欧拉回路充要条件:G为无奇度结点的连通图. 思路:推断是否存在欧拉通路.依据欧拉通路.欧拉回路的性质来做.有两种情况:一种是欧拉回路.全部房间的门的个数都是偶数个,而且此时初始房间不

hdu 1116 Play on Words(欧拉通路)

Problem Description Some of the secret doors contain a very interesting word puzzle. The team of archaeologists has to solve it to open that doors. Because there is no other way to open the doors, the puzzle is very important for us. There is a large

POJ 2337 Catenyms (有向图欧拉通路)

Catenyms Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9914   Accepted: 2588 Description A catenym is a pair of words separated by a period such that the last letter of the first word is the same as the last letter of the second. For e

POJ--1300--Door Man【判断欧拉通路】

链接:http://poj.org/problem?id=1300 题意:有n个房间,每个房间有若干个门和别的房间相连,管家从m房间开始走,要回到自己的住处(0),问是否有一条路可以走遍所有的门并且没有重复的路. 思路:判断是否存在欧拉通路,根据欧拉通路.欧拉回路的性质来做.有两种情况:一种是欧拉回路,所有房间的门的个数都是偶数个,并且此时初始房间不是0,此时存在要求的路径,如果初始是0则不行.另一种是欧拉通路,只有两个房间门是奇数个,剩下都是偶数个,并且这两个房间一个是0,一个是当前起点,并且

hdu3472 混合图判断欧拉通路

对于欧拉回路,先判断出度入度的差是否为偶数,然后最大流一次. 此题是判断有无欧拉通路,前提要判断图是否连通,然后欧拉通路的条件:要么出入度差没有奇数,或者只有2个点. 所以先统计差为奇数的个数,如果不为0或2,不可能.然后如果为2,表示可能使欧拉路,所以此时可以将这两个点相连,类似添加一条无向边.然后就是判断是否为欧拉回路了. #include<stdio.h> #include<string.h> #include<queue> #define maxn 50 #de