【题目链接】:http://codeforces.com/problemset/problem/508/D

【题意】



给你一个字符的所有连续3个的子串;

让你复原出原串;

(包含小写、大写字母以及数字);

【题解】



相当于给你n条边;

每条边的两端;

一个点是由s[0]s[1]组成的;

另一端是s[1]s[2]组成的;

(用map很容易做hash..)

然后让你求一个欧拉通路;

即经过所有的边仅一次的路径(一笔画);

有向图;

可以先算出每个点的入度和出度;

设入度和出度的差的点数为1的个数为cnt;

则只有当cnt为2或cnt为0的时候有解;

当cnt==2时,其中出度-入度==1的点作为起点,出度-入度==-1的点作终点;

当cnt==0时,随便取个点当起点;

然后做fleury算法就好;

(求出来的是逆序的,倒一下就好)

讲得比较好的

↓↓







看了上面的分析;

可以知道;

在搜一个点的时候,往前走的同时删边;

然后一条路走到黑;

把中间没有出度的点输出就好(倒序的);

然后对于中间还有出度的点再一条道走到黑就好(肯定能回来的);

然后重复上述步骤;

得到倒序的欧拉路径;



【Number Of WA】



0



【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ms(x,y) memset(x,y,sizeof x)
#define Open() freopen("F:\\rush.txt","r",stdin)
#define Close() ios::sync_with_stdio(0),cin.tie(0) typedef pair<int,int> pii;
typedef pair<LL,LL> pll; const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int N = 2e5+100; int n,tot,in[N*2],out[N*2],f[N*2],s,t;
char S[4];
map <string,int> dic;
string is[N*2];
vector <int> G[N*2];
stack <int> sta;
string ans=""; int ff(int x)
{
if (f[x]!=x)
return f[x] = ff(f[x]);
else
return x;
} void wujie()
{
cout <<"NO"<<endl;
exit(0);
} void dfs(int x)
{
while (G[x].size())
{
//cout << x << endl;
sta.push(x);
int y = G[x][G[x].size()-1];
G[x].pop_back();
x = y;
}
sta.push(x);
} void oula(int s)
{
sta.push(s);
int flag = 0;
while (!sta.empty())
{
int x = sta.top();
int bridge = (G[x].size()==0);
sta.pop();
if (!bridge)
{
dfs(x);
}
else
{
if (!flag)
{
ans+=is[x][1];
ans+=is[x][0];
flag = 1;
}
else
ans+=is[x][0];
}
}
} int main()
{
//Open();
Close();//scanf,puts,printf not use
//init??????
cin >> n;
string s1,s2;
rep1(i,1,2*n)
f[i] = i;
rep1(i,1,n)
{
cin >> S;
s1 = S[0];
s1+=S[1];
s2 = S[1];
s2+=S[2];
if (dic[s1]==0)
tot++,dic[s1] = tot,is[tot] = s1;
if (dic[s2]==0)
tot++,dic[s2] = tot,is[tot] = s2;
int x = dic[s1],y = dic[s2];
in[y]++,out[x]++;
G[x].pb(y);
int r1 = ff(x),r2 = ff(y);
if (r1!=r2)
f[r1] = r2;
}
n = tot;
int root = ff(1);
rep1(i,1,tot)
if (ff(i)!=root)
wujie();
int cnt = 0;
rep1(i,1,n)
{
if (in[i]==out[i]) continue;
cnt++;
if (in[i]-out[i]==-1)
s = i;
else
if (in[i]-out[i]==1)
t = i;
else
wujie();
}
//cout << tot <<endl;
//cout <<is[1]<<endl;
//cout <<is[2]<<endl;
//cout <<is[3]<<endl;
//cout <<is[4]<<endl;
//cout <<"s="<<s<<' '<<"t="<<t<<endl;
//return 0;
if ( (cnt==2 && s!=0 && t!=0) || (cnt==0))
{
cout <<"YES"<<endl;
if (cnt==0) s = 1;
oula(s);
reverse(ans.begin(),ans.end());
cout << ans << endl;
}
else
wujie();
return 0;
}

【codeforces 508D】Tanya and Password的更多相关文章

  1. 【codeforces 761C】Dasha and Password(动态规划做法)

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  2. 【codeforces 761C】Dasha and Password(贪心+枚举做法)

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  3. 【codeforces 508D】The Maths lecture

    [题目链接]:http://codeforces.com/problemset/problem/507/D [题意] 让你找符合这样数字的数的个数: 1.有n个数码 2.某个后缀%k的值为0 3.大于 ...

  4. 【codeforces 415D】Mashmokh and ACM(普通dp)

    [codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...

  5. 【codeforces 707E】Garlands

    [题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...

  6. 【codeforces 707C】Pythagorean Triples

    [题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...

  7. 【codeforces 709D】Recover the String

    [题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...

  8. 【codeforces 709B】Checkpoints

    [题目链接]:http://codeforces.com/contest/709/problem/B [题意] 让你从起点开始走过n-1个点(至少n-1个) 问你最少走多远; [题解] 肯定不多走啊; ...

  9. 【codeforces 709C】Letters Cyclic Shift

    [题目链接]:http://codeforces.com/contest/709/problem/C [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...

随机推荐

  1. 【转载】99%的人都理解错了HTTP中GET与POST的区别

    作者:Larry链接:https://zhuanlan.zhihu.com/p/22536382来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 原文链接 作者:WebT ...

  2. 二分图建图,并查集求联通——二维等价性传递 cf1012B好题!

    /* 模拟二分图:每个点作为一条边,连接的是一列和一行(抽象成一个点,列在左,行在右) 由题意得 a-b相连,a-c相连,b-d相连,那么d-c就不用再相连了 等价于把二分图变成联通的需要再加多少边 ...

  3. 怎样配置duilib

    duilib是一个免费的界面库,它可利用xml文件自定义界面元素,并且可以在商业项目中无偿使用.怎样在VS中配置duilib界面库呢?请看下面的介绍. 工具/原料 duilib 下载和编译duilib ...

  4. ECMAScript 6中的Set和Map数据结构

    一.Set 基本用法: Set本身是一个构造函数,用来生成Set数据结构.Set函数可以接受一个数组作为参数用来初始化. const arr = new Set([2,2,3,3,4,4,5,8]); ...

  5. (4)mysql约束

    完整性约束 1.设置非空约束 not null 简称 NK约束 创建表时设置非空 CREATE TABLE tb_meng2(id INT NOT NULL,age INT); 2.设置字段默认值 默 ...

  6. play framework 从环境搭建到简单运行

    download 官网:https://www.playframework.com/ 将zip下载至本地,进行unzip. 环境变量 检测是否安装成功:解压成功后进入解压的目录,运行 play 终端显 ...

  7. PAT甲级——A1119 Pre- and Post-order Traversals【30】

    Suppose that all the keys in a binary tree are distinct positive integers. A unique binary tree can ...

  8. <每日一题>题目11:以文件夹名称作为参数,返回该文件夹下所有文件的路径

    ''' 分析: 1.知道文件夹名称(假设是形如:E:\\software\\Notepad++),很显然可以通过OS模块去求 2.OS.listdir(sPath),列出文件夹内所有的文件和文件夹,以 ...

  9. [Vue warn]: Failed to mount component: template or render function not defined. 错误解决方法

    解决方法import Vue from "vue"; 默认引入的文件是 vue/dist/vue.runtime.common.js.这个可以在node_modules/vue/p ...

  10. day1-字符串、列表

    字符串操作: name = "Wills Qian" # 创建字符串变量 print(len(name)) # 打印字符串长度 print(name[0]) # 提取第一个字符W ...