CodeForces 508D Tanya and Password(欧拉路径)

2024-03-27 23:32

本文主要是介绍CodeForces 508D Tanya and Password(欧拉路径),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

题目:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=111980

Tanya and Password
Time Limit: 2000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u

Submit Status

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 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.

Input

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.

Output

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.

Sample Input

Input
5
aca
aba
aba
cab
bac
Output
YES
abacaba
Input
4
abc
bCb
cb1
b13
Output
NO
Input
7
aaa
aaa
aaa
aaa
aaa
aaa
aaa
Output
YES
aaaaaaaaa
这是有向图的欧拉通路问题,虽然我能看出来,但是输出问题和存储问题没有很好解决。参考了大神们的代码:发现他们用0,1字符和1,2字符作为线段端点的数字信息。妙!输出则是DFS。
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int N1=2e5+5,N2=4e3; // (26*2+10)^2<4000
int f[N2],in[N2],out[N2],path[N1],e[N2][N2];
bool has[N2];
int len,st,n;
struct node{int u,v;char name[5];
}snode[N1];
int abs(int x){return x>0?x:(-x);
}
void init(){for(int i=0;i<N2;i++) f[i]=i;memset(in,0,sizeof(in));memset(out,0,sizeof(out));memset(path,0,sizeof(path));memset(e,0,sizeof(e));memset(has,0,sizeof(has));
}
int find(int x){if(x==f[x]) return x;return f[x]=find(f[x]);
}
int ctoi(char ch){if(ch<='9'&&ch>='0') return ch-'0';if(ch>='A'&&ch<='Z') return ch-'A'+10;return ch-'a'+36;
}
char itoc(int x){if(x<=9&&x>=0) return x+'0';if(x<=35&&x>=10) return x+'A'-10;return x+'a'-36;
}
bool Exist(){int t=-1,sum=0,temp;for(int i=0;i<N2;i++){if(has[i]){if(t==-1) t=find(i);else if(find(i)!=t)  return 0;}}for(int i=0;i<N2;i++){if(has[i]){temp=i;if(in[i]!=out[i]){sum++;if(abs(in[i]-out[i])>1) return 0;if(out[i]>in[i]) st=i; //链的端点}}}if(sum>2) return 0;if(sum==0) st=temp;  // 环的一节点return 1;
}
void dfs(int q){for(int i=0;i<N2;i++){while(e[q][i]){e[q][i]--;dfs(i);path[len++]=i;}}
}
int main()
{//freopen("cin.txt","r",stdin);int n;while(cin>>n){init();for(int i=0;i<n;i++){scanf("%s",snode[i].name);snode[i].u=ctoi(snode[i].name[0])*62+ctoi(snode[i].name[1]);snode[i].v=ctoi(snode[i].name[1])*62+ctoi(snode[i].name[2]);int a=snode[i].u,b=snode[i].v;has[a]=has[b]=1;f[find(a)]=f[find(b)];out[a]++;in[b]++;e[a][b]++;}if(!Exist()) puts("NO");else {len=0;dfs(st);path[len++]=st;puts("YES");//cout<<path[len-1]<<endl;printf("%c%c",itoc(path[len-1]/62),itoc(path[len-1]%62));for(int i=len-2;i>=0;i--){printf("%c",itoc(path[i]%62));}puts("");}}return 0;
}


这篇关于CodeForces 508D Tanya and Password(欧拉路径)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/853764

相关文章

Linux修改pip和conda缓存路径的几种方法

《Linux修改pip和conda缓存路径的几种方法》在Python生态中,pip和conda是两种常见的软件包管理工具,它们在安装、更新和卸载软件包时都会使用缓存来提高效率,适当地修改它们的缓存路径... 目录一、pip 和 conda 的缓存机制1. pip 的缓存机制默认缓存路径2. conda 的缓

Windows系统下如何查找JDK的安装路径

《Windows系统下如何查找JDK的安装路径》:本文主要介绍Windows系统下如何查找JDK的安装路径,文中介绍了三种方法,分别是通过命令行检查、使用verbose选项查找jre目录、以及查看... 目录一、确认是否安装了JDK二、查找路径三、另外一种方式如果很久之前安装了JDK,或者在别人的电脑上,想

Python中Windows和macOS文件路径格式不一致的解决方法

《Python中Windows和macOS文件路径格式不一致的解决方法》在Python中,Windows和macOS的文件路径字符串格式不一致主要体现在路径分隔符上,这种差异可能导致跨平台代码在处理文... 目录方法 1:使用 os.path 模块方法 2:使用 pathlib 模块(推荐)方法 3:统一使

一文教你解决Python不支持中文路径的问题

《一文教你解决Python不支持中文路径的问题》Python是一种广泛使用的高级编程语言,然而在处理包含中文字符的文件路径时,Python有时会表现出一些不友好的行为,下面小编就来为大家介绍一下具体的... 目录问题背景解决方案1. 设置正确的文件编码2. 使用pathlib模块3. 转换路径为Unicod

MySQL9.0默认路径安装下重置root密码

《MySQL9.0默认路径安装下重置root密码》本文主要介绍了MySQL9.0默认路径安装下重置root密码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们... 目录问题描述环境描述解决方法正常模式下修改密码报错原因问题描述mysqlChina编程采用默认安装路径,

python获取当前文件和目录路径的方法详解

《python获取当前文件和目录路径的方法详解》:本文主要介绍Python中获取当前文件路径和目录的方法,包括使用__file__关键字、os.path.abspath、os.path.realp... 目录1、获取当前文件路径2、获取当前文件所在目录3、os.path.abspath和os.path.re

hdu2544(单源最短路径)

模板题: //题意:求1到n的最短路径,模板题#include<iostream>#include<algorithm>#include<cstring>#include<stack>#include<queue>#include<set>#include<map>#include<stdio.h>#include<stdlib.h>#include<ctype.h>#i

poj 1734 (floyd求最小环并打印路径)

题意: 求图中的一个最小环,并打印路径。 解析: ans 保存最小环长度。 一直wa,最后终于找到原因,inf开太大爆掉了。。。 虽然0x3f3f3f3f用memset好用,但是还是有局限性。 代码: #include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#incl

uva 1342 欧拉定理(计算几何模板)

题意: 给几个点,把这几个点用直线连起来,求这些直线把平面分成了几个。 解析: 欧拉定理: 顶点数 + 面数 - 边数= 2。 代码: #include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <cstring>#include <cmath>#inc

Codeforces Round #240 (Div. 2) E分治算法探究1

Codeforces Round #240 (Div. 2) E  http://codeforces.com/contest/415/problem/E 2^n个数,每次操作将其分成2^q份,对于每一份内部的数进行翻转(逆序),每次操作完后输出操作后新序列的逆序对数。 图一:  划分子问题。 图二: 分而治之,=>  合并 。 图三: 回溯: