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

相关文章

python获取指定名字的程序的文件路径的两种方法

《python获取指定名字的程序的文件路径的两种方法》本文主要介绍了python获取指定名字的程序的文件路径的两种方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要... 最近在做项目,需要用到给定一个程序名字就可以自动获取到这个程序在Windows系统下的绝对路径,以下

SpringBoot路径映射配置的实现步骤

《SpringBoot路径映射配置的实现步骤》本文介绍了如何在SpringBoot项目中配置路径映射,使得除static目录外的资源可被访问,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一... 目录SpringBoot路径映射补:springboot 配置虚拟路径映射 @RequestMapp

python设置环境变量路径实现过程

《python设置环境变量路径实现过程》本文介绍设置Python路径的多种方法:临时设置(Windows用`set`,Linux/macOS用`export`)、永久设置(系统属性或shell配置文件... 目录设置python路径的方法临时设置环境变量(适用于当前会话)永久设置环境变量(Windows系统

Spring Boot中的路径变量示例详解

《SpringBoot中的路径变量示例详解》SpringBoot中PathVariable通过@PathVariable注解实现URL参数与方法参数绑定,支持多参数接收、类型转换、可选参数、默认值及... 目录一. 基本用法与参数映射1.路径定义2.参数绑定&nhttp://www.chinasem.cnbs

SpringBoot项目配置logback-spring.xml屏蔽特定路径的日志

《SpringBoot项目配置logback-spring.xml屏蔽特定路径的日志》在SpringBoot项目中,使用logback-spring.xml配置屏蔽特定路径的日志有两种常用方式,文中的... 目录方案一:基础配置(直接关闭目标路径日志)方案二:结合 Spring Profile 按环境屏蔽关

VSCode设置python SDK路径的实现步骤

《VSCode设置pythonSDK路径的实现步骤》本文主要介绍了VSCode设置pythonSDK路径的实现步骤,包括命令面板切换、settings.json配置、环境变量及虚拟环境处理,具有一定... 目录一、通过命令面板快速切换(推荐方法)二、通过 settings.json 配置(项目级/全局)三、

使用Python和Matplotlib实现可视化字体轮廓(从路径数据到矢量图形)

《使用Python和Matplotlib实现可视化字体轮廓(从路径数据到矢量图形)》字体设计和矢量图形处理是编程中一个有趣且实用的领域,通过Python的matplotlib库,我们可以轻松将字体轮廓... 目录背景知识字体轮廓的表示实现步骤1. 安装依赖库2. 准备数据3. 解析路径指令4. 绘制图形关键

如何更改pycharm缓存路径和虚拟内存分页文件位置(c盘爆红)

《如何更改pycharm缓存路径和虚拟内存分页文件位置(c盘爆红)》:本文主要介绍如何更改pycharm缓存路径和虚拟内存分页文件位置(c盘爆红)问题,具有很好的参考价值,希望对大家有所帮助,如有... 目录先在你打算存放的地方建四个文件夹更改这四个路径就可以修改默认虚拟内存分页js文件的位置接下来从高级-

一文详解如何查看本地MySQL的安装路径

《一文详解如何查看本地MySQL的安装路径》本地安装MySQL对于初学者或者开发人员来说是一项基础技能,但在安装过程中可能会遇到各种问题,:本文主要介绍如何查看本地MySQL安装路径的相关资料,需... 目录1. 如何查看本地mysql的安装路径1.1. 方法1:通过查询本地服务1.2. 方法2:通过MyS

Python如何调用指定路径的模块

《Python如何调用指定路径的模块》要在Python中调用指定路径的模块,可以使用sys.path.append,importlib.util.spec_from_file_location和exe... 目录一、sys.path.append() 方法1. 方法简介2. 使用示例3. 注意事项二、imp