Codeforces #445 Div2 D
#445 Div2 D
题意
给出一些字符串,要求构造一个最短的且字典序最小的字符串,使得给出的字符串都为目标字符串的子串,且这些字符串作为子串出现的次数都是最多的,如果不存在目标字符串输出 "NO"。
分析
显然,每个字符只能出现一次。
然后,一个长度为 \(l\) 的字符串,其实指明了 \(l-1\) 个连边关系,如字符串 ab ,那么 \(a\) 的后面一定只能为 \(b\) 所以 \(a\) \(b\) 连有向边,且只能有一条边连向 \(b\) ,\(a\) 只能连一条边出去。这个想法确立了,后面就好做了。
code
#include<bits/stdc++.h>
using namespace std;
int nxt[130], vis[130], f[130];
char s[111111];
int main() {
int n;
cin >> n;
int flg = 1;
memset(nxt, -1, sizeof nxt);
for(int i = 0; i < n; i++) {
scanf("%s", s);
if(f[s[0]] != -1) f[s[0]] = 1;
vis[s[0]] = 1;
int len = strlen(s);
for(int j = 1; j < len; j++) {
int c = s[j];
int &x = nxt[s[j - 1]];
f[c] = -1;
if(x == -1) x = c;
else if(x != c) flg = 0;
vis[c] = 1;
}
}
if(!flg) cout << "NO" << endl;
else {
int cnt = 0;
for(int i = 'a'; i <= 'z'; i++) {
if(vis[i] && f[i] == 1) {
int t = i;
while(t != -1) {
vis[t] = 0;
s[cnt++] = t;
t = nxt[t];
if(t != -1 && vis[t] == 0) {
flg = 0;
break;
}
}
}
}
for(int i = 'a'; i <= 'z'; i++) {
if(vis[i]) flg = 0;
}
s[cnt] = 0;
if(!flg) cout << "NO" << endl;
else cout << s << endl;
}
return 0;
}
Codeforces #445 Div2 D的更多相关文章
- Codeforces #180 div2 C Parity Game
// Codeforces #180 div2 C Parity Game // // 这个问题的意思被摄物体没有解释 // // 这个主题是如此的狠一点(对我来说,),不多说了这 // // 解决问 ...
- Codeforces #541 (Div2) - E. String Multiplication(动态规划)
Problem Codeforces #541 (Div2) - E. String Multiplication Time Limit: 2000 mSec Problem Descriptio ...
- Codeforces #541 (Div2) - F. Asya And Kittens(并查集+链表)
Problem Codeforces #541 (Div2) - F. Asya And Kittens Time Limit: 2000 mSec Problem Description Inp ...
- Codeforces #541 (Div2) - D. Gourmet choice(拓扑排序+并查集)
Problem Codeforces #541 (Div2) - D. Gourmet choice Time Limit: 2000 mSec Problem Description Input ...
- Codeforces #548 (Div2) - D.Steps to One(概率dp+数论)
Problem Codeforces #548 (Div2) - D.Steps to One Time Limit: 2000 mSec Problem Description Input Th ...
- 【Codeforces #312 div2 A】Lala Land and Apple Trees
# [Codeforces #312 div2 A]Lala Land and Apple Trees 首先,此题的大意是在一条坐标轴上,有\(n\)个点,每个点的权值为\(a_{i}\),第一次从原 ...
- Codeforces #263 div2 解题报告
比赛链接:http://codeforces.com/contest/462 这次比赛的时候,刚刚注冊的时候非常想好好的做一下,可是网上喝了个小酒之后.也就迷迷糊糊地看了题目,做了几题.一觉醒来发现r ...
- codeforces #round363 div2.C-Vacations (DP)
题目链接:http://codeforces.com/contest/699/problem/C dp[i][j]表示第i天做事情j所得到最小的假期,j=0,1,2. #include<bits ...
- codeforces round367 div2.C (DP)
题目链接:http://codeforces.com/contest/706/problem/C #include<bits/stdc++.h> using namespace std; ...
随机推荐
- [Leetcode] Best time to buy and sell stock 买卖股票的最佳时机
Say you have an array for which the i th element is the price of a given stock on day i. If you were ...
- CSS3中transform属性的用法
有时候网站也要愚弄一下访客,比如愚人节.下面我给大家推荐个效果,就是整个页面左右颠倒了.css3 很强大,简单的几行代码就可以帮我们实现这个效果. view source print? 01 &l ...
- [uva 1350]数位dp+二分
题目链接:https://vjudge.net/problem/38405 #include<bits/stdc++.h> using namespace std; ][]; ]; lon ...
- tcp/ip网络协议学习
链路层介绍 网络层协议的数据单元是 IP 数据报 ,而数据链路层的工作就是把网络层交下来的 IP 数据报 封装为 帧(frame)发送到链路上,以及把接收到的帧中的数据取出并上交给网络层. 以太网 以 ...
- AngularJs学习——何时应该使用Directive、Controller、Service?
翻译:大漠穷秋 原文链接:http://kirkbushell.me/when-to-use-directives-controllers-or-services-in-angular/ 一.简述 A ...
- 前端跨域之jsonp跨域
jsonp跨域原理 原理:因为通过script标签引入的js是不受同源策略的限制的(比如baidu.com的页面加载了google.com的js).所以我们可以通过script标签引入一个js或者一个 ...
- Android百度定位地图使用--文章集锦
Android百度定位API使用方法 Android百度地图开发(一)之初体验 AndroidNote013.在百度地图上画出轨迹 Android学习笔记之百度地图(分条目覆盖物:ItemizedOv ...
- threadlocal作用
理解:通过thread创建局部变量,每个线程可以获得该变量的副本,再每个线程中操作该副本相互之间不产生影响. 解决:数据库连接 常规一个线程连接一个数据库是没有问题的,但是在高并发的情况下,可能线程一 ...
- 【Codeforces】849D. Rooter's Song
[算法]模拟 [题意]http://codeforces.com/contest/849/problem/D 给定n个点从x轴或y轴的位置p时间t出发,相遇后按对方路径走,问每个数字撞到墙的位置.(还 ...
- 之江学院第0届校赛 qwb去面试 (找规律)
Description 某一天,qwb去WCfun面试,面试官问了他一个问题:把一个正整数n拆分成若干个正整数的和,请求出这些数乘积的最大值. qwb比较猥琐,借故上厕所偷偷上网求助,聪明的你能帮助他 ...