#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的更多相关文章

  1. Codeforces #180 div2 C Parity Game

    // Codeforces #180 div2 C Parity Game // // 这个问题的意思被摄物体没有解释 // // 这个主题是如此的狠一点(对我来说,),不多说了这 // // 解决问 ...

  2. Codeforces #541 (Div2) - E. String Multiplication(动态规划)

    Problem   Codeforces #541 (Div2) - E. String Multiplication Time Limit: 2000 mSec Problem Descriptio ...

  3. Codeforces #541 (Div2) - F. Asya And Kittens(并查集+链表)

    Problem   Codeforces #541 (Div2) - F. Asya And Kittens Time Limit: 2000 mSec Problem Description Inp ...

  4. Codeforces #541 (Div2) - D. Gourmet choice(拓扑排序+并查集)

    Problem   Codeforces #541 (Div2) - D. Gourmet choice Time Limit: 2000 mSec Problem Description Input ...

  5. Codeforces #548 (Div2) - D.Steps to One(概率dp+数论)

    Problem   Codeforces #548 (Div2) - D.Steps to One Time Limit: 2000 mSec Problem Description Input Th ...

  6. 【Codeforces #312 div2 A】Lala Land and Apple Trees

    # [Codeforces #312 div2 A]Lala Land and Apple Trees 首先,此题的大意是在一条坐标轴上,有\(n\)个点,每个点的权值为\(a_{i}\),第一次从原 ...

  7. Codeforces #263 div2 解题报告

    比赛链接:http://codeforces.com/contest/462 这次比赛的时候,刚刚注冊的时候非常想好好的做一下,可是网上喝了个小酒之后.也就迷迷糊糊地看了题目,做了几题.一觉醒来发现r ...

  8. codeforces #round363 div2.C-Vacations (DP)

    题目链接:http://codeforces.com/contest/699/problem/C dp[i][j]表示第i天做事情j所得到最小的假期,j=0,1,2. #include<bits ...

  9. codeforces round367 div2.C (DP)

    题目链接:http://codeforces.com/contest/706/problem/C #include<bits/stdc++.h> using namespace std; ...

随机推荐

  1. [bzoj] 2694 Lcm || 莫比乌斯反演

    原题 定义整数a,b,求所有满足条件的lcm(a,b)的和: 1<=a<=A 1<=b<=B ∀n>1,n2†gcd(a,b)(即任意n>1,\(n^2\)不是gc ...

  2. 【BZOJ 2744 朋友圈】

    Time Limit: 30 Sec  Memory Limit: 128 MBSubmit: 1570  Solved: 532[Submit][Status][Discuss] Descripti ...

  3. 微信小程序使用Socket

    首先,一个小程序同时只能有一个WebSocket连接,如果当前已经存在一个WebSocket连接,会关闭当前连接,并重新建立一个连接. 其次,如果使用了appID,协议必须是 wss://... 最近 ...

  4. JS让任意图片垂直水平居中且页面不滚动

    说一下以前遇到的一个问题: 假设有一张小图,要实现点击查看大图的功能,而这个图的宽高可能会超过浏览器的宽高,这时候我们通过JS来改变图片的宽高,从而实现图片在浏览器居中显示且不滚屏. 方法如下: 首先 ...

  5. CentOS ninimal 安装后没有桌面-yellowcong

    昨天,安装Centos后,发现没有桌面,主要是没有安装桌面环境导致 的这个问题,我们需要做的第一步是,安装一个桌面(GNOME Desktop,命令:yum groupinstall -y " ...

  6. 从零开始学习MXnet(五)MXnet的黑科技之显存节省大法

    写完发现名字有点拗口..- -# 大家在做deep learning的时候,应该都遇到过显存不够用,然后不得不去痛苦的减去batchszie,或者砍自己的网络结构呢? 最后跑出来的效果不尽如人意,总觉 ...

  7. php-instanceof运算符

    1.关于 instanceof 的一些基本概念 1).instanceof 用于确定一个PHP变量是否属于某一类class的实例: <?php class MyClass { } class N ...

  8. 修改nginx对http请求数据大小限制

    1. 问题发现 在公司搭建了一个基于mindoc的wiki知识库,用nginx做的反向代理服务器,同事在使用过程中上传某个文件一直失败,于是看着看下mindoc自己的日志文件,发现都是类似于fastd ...

  9. 如何打开小米,oppo,华为等手机的系统应用的指定页面

    如题,拿Oppo 手机做个示例,小米 华为也是如此. 在编写Android应用的时候,我们经常会有这样的需求,我们想直接打开系统应用的某个页面.比如在Oppo R9 手机上我们想打开某个应用的通知管理 ...

  10. The service base of EF I am using

    using CapMon.Data; using System; using System.Collections.Generic; using System.Linq; using System.T ...