【链接】 我是链接,点我呀:)

【题意】

给你n个字符串。
让你构造一个字符串s。
使得这n个字符串。
每个字符串都是s的子串。
且都是出现次数最多的子串。
要求s的长度最短,且s的字典序最小。

【题解】

如果s是出现最多的子串。
那么s的任意一个子串也都是出现次数最多的子串。
那么考虑"ab"这样一个子串。
则肯定要有字符'a'后面接的一定是'b'
且字符'b'前接的一定是'a'
不然'a'或'b'的出现次数一定会大于"ab"了。
则对于任意一个字符串s,在s[i]和s[i+1]之间建立一条有向边,s[i]->s[i+1];
这就是一个类似拓扑排序的题了。
(如果没办法做拓扑排序,则无解
但是有一定要求。
即每个点的出度和入度一定要是1.
否则,如果有a->b,c->b这种情况。
拓扑排序救过是acb,但是没办法满足a后面紧跟着b.
还有a->c,a->b这种也显然不行,也没办法两者都满足。
然后要优先拓扑有边的点。
比如
"c"
"bd"
则b进入答案之后,下一个应该拓扑的是d而不是"c",因为d是要紧跟在b后面的1

【代码】

#include <bits/stdc++.h>
using namespace std; int n;
string s;
bool bo[300];
int a[300][300],rd[300],cd[300],tot = 26;
int inq[300],special;
queue <int> dl;
string ans = ""; int main(){
#ifdef LOCAL_DEFINE
freopen("F:\\c++source\\rush_in.txt", "r", stdin);
#endif
ios::sync_with_stdio(0),cin.tie(0);
cin >> n;
for (int i = 1;i <= n;i++){
cin >> s;
int len = s.size();
for (int j = 0;j < len;j++) bo[(int)s[j]] = true;
for (int j = 0;j < len-1;j++){
if (a[(int)s[j]][(int)s[j+1]]==0) {
rd[(int)s[j+1]]++;
cd[(int) s[j]]++;
}
a[(int)s[j]][(int)s[j+1]] = 1;
}
} for (int key = 'a';key <= 'z';key++)
if (!bo[key]){
rd[key] = cd[key] = -1;
tot--;
} for (int key = 'a';key <= 'z';key++)
if (rd[key]==0){
inq[key] = 1;
rd[key] = -1;
if (cd[key] > 1) return cout <<"NO"<<endl,0;
}else {
if (rd[key]>1 || cd[key] >1) return cout <<"NO"<<endl,0;
} special = 0;
while (1){
int x = -1;
if (special!=0) x = special;
for (int key = 'a';x==-1 && key <= 'z';key++)
if (inq[key]){
x = key;
break;
}
if (x==-1) break;
tot--;
inq[x] = 0;
if (special==x) special = 0;
ans += (char) x;
for (int i = 'a';i <= 'z';i++){
if (a[x][i]){
rd[i]--;
a[x][i] = 0;
if (rd[i]==0){
special = i;
inq[i] = 1;
rd[i] = -1;
}else{
return cout <<"NO"<<endl,0;
}
}
}
} if (tot!=0){
cout <<"NO"<<endl;
}else{
cout << ans << endl;
}
return 0;
}

【Codeforces Round #445 (Div. 2) D】Restoration of string的更多相关文章

  1. 【Codeforces Round #445 (Div. 2) C】 Petya and Catacombs

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 看看时间戳为i的点有哪些. 每次优先用已经访问过的点. 如果不行就新创一个点. 注意新创点的时间戳也是i. [代码] #includ ...

  2. 【Codeforces Round #445 (Div. 2) B】Vlad and Cafes

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 傻逼模拟 [代码] #include <bits/stdc++.h> using namespace std; cons ...

  3. 【Codeforces Round #445 (Div. 2) A】ACM ICPC

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 三重循环 [代码] #include <bits/stdc++.h> using namespace std; int ...

  4. 【Codeforces Round #442 (Div. 2) B】Nikita and string

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 枚举中间那一段从哪里开始.哪里结束就好 注意为空的话,就全是a. 用前缀和优化一下. [代码] #include <bits/ ...

  5. 【Codeforces Round #432 (Div. 1) B】Arpa and a list of numbers

    [链接]h在这里写链接 [题意] 定义bad list是一个非空的.最大公约数为1的序列.给定一个序列,有两种操作:花费x将一个元素删除.花费y将一个元素加1,问你将这个序列变为good list所需 ...

  6. 【Codeforces Round #420 (Div. 2) C】Okabe and Boxes

    [题目链接]:http://codeforces.com/contest/821/problem/C [题意] 给你2*n个操作; 包括把1..n中的某一个数压入栈顶,以及把栈顶元素弹出; 保证压入和 ...

  7. 【Codeforces Round #420 (Div. 2) B】Okabe and Banana Trees

    [题目链接]:http://codeforces.com/contest/821/problem/B [题意] 当(x,y)这个坐标中,x和y都为整数的时候; 这个坐标上会有x+y根香蕉; 然后给你一 ...

  8. 【Codeforces Round #420 (Div. 2) A】Okabe and Future Gadget Laboratory

    [题目链接]:http://codeforces.com/contest/821/problem/A [题意] 给你一个n*n的数组; 然后问你,是不是每个位置(x,y); 都能找到一个同一行的元素q ...

  9. 【Codeforces Round #423 (Div. 2) C】String Reconstruction

    [Link]:http://codeforces.com/contest/828/problem/C [Description] 让你猜一个字符串原来是什么; 你知道这个字符串的n个子串; 且知道第i ...

随机推荐

  1. CF 439C(251C题)Devu and Partitioning of the Array

    Devu and Partitioning of the Array time limit per test 1 second memory limit per test 256 megabytes ...

  2. PhoneGap/Cordova Android应用签名公布注意事项

    今天最终要公布Android HybirdApp了,安装曾经做原生应用的流程公布签名Apk,没想到立即遇到了几个问题.如今把它们的解决的方法整理下来. export signed Apk 遇到以下错误 ...

  3. android启动模式对于体验的影响

    说到Android的启动模式.懂Android的人肯定都懂. 通过设置启动模式我们不仅能够节省内存的使用.还能达到更好的体验,比方我们打开一个应用,点击home键回到主界面的时候程序是没有被kill掉 ...

  4. Android 给图片 加边框

    图片处理时,有时需要为图片加一些边框,下面介绍一种为图片添加简单边框的方法. 基本思路是:将边框图片裁剪成八张小图片(图片大小最好一致,不然后面处理会很麻烦),分别对应左上角,左边,左下角,下边,右下 ...

  5. C#制作文本转换为声音的demo,保存音频文件到本地

    TTS(Text To Speech)可以实现把文本转换成语音并朗读出来.Windows Xp可以使用Com组件--Microsoft Speech Object Library实现TTS,Windo ...

  6. vue -- 跨域cookie 丢失的问题

    前端使用了vue-reource的$http进行请求后台接口 登陆完成后,服务端监控发现无法拿到cookie,下面看几张前端控制台监控的图 reqqust Header  没有显示cookie 信息 ...

  7. 给iOS项目中添加图片,并通过UIImageView引用和显示该UIImage图片

    [问题] 关于iOS/iPhone中的文件选择对话框,用于用户去选择图片等文件 过程中,问题转换为,需要给当前iOS项目中,添加一个图片. 类似于Windows开发中的资源文件,其中图片文件属于资源的 ...

  8. Self-Taught Learning to Deep Networks

    In this section, we describe how you can fine-tune and further improve the learned features using la ...

  9. Kinect 开发 —— Hello,Kinect

    控制台输出深度数据: using System; using System.Collections.Generic; using System.Linq; using System.Text; usi ...

  10. vue中computed与watch的异同

    一.computed 和 watch   都可以观察页面的数据变化.当处理页面的数据变化时,我们有时候很容易滥用watch. 而通常更好的办法是使用computed属性,而不是命令是的watch回调. ...