#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. 【题解】CQOI2015选数

    这题做的时候接连想错了好多次……但是回到正轨上之后依然是一个套路题.(不过这题好像有比莫比乌斯反演更好的做法,莫比乌斯反演貌似是某种能过的暴力ヽ(´ー`)┌)不过能过也就行了吧哈哈. 首先我们把数字的 ...

  2. linux kernel 关于RSS/RPS/RFS/XPS的介绍

    Introduction============ This document describes a set of complementary techniques in the Linuxnetwo ...

  3. 【BZOJ 3172】[Tjoi2013]单词 AC自动机

    关于AC自动机:一个在kmp与Trie的基础上建立的数据结构,关键在于Trie树结构与fail指针,他们各有各的应用.在AC自动机里最典型的就是多串匹配,原本效率为O(n*l+n*l+m*l),(n是 ...

  4. [zhuan]Android安全讲座第九层(二) 内存dump

    http://sunzeduo.blog.51cto.com/2758509/1409450 近来android上越来越多的应用对自身的保护机制加强了重视,主要表现在几个方面. 1 dex加壳 2 s ...

  5. Array.slice(start,end)的用法

    start在start>=0,假设start=0,表示从数组的第一个元素开始截取,start=2,表示从数组的第二个元素开始截取,依次类推. 在start<0时,start=-1表示从倒数 ...

  6. npm获取配置值的两种方式

    命令行标记 在命令行上放置--foo bar设置foo配置参数为bar. 一个 -- 参数(argument)告诉cli解析器停止读取flags.一个 在命令行结尾的--flag参数(paramete ...

  7. java 身份证15位转18位

    /** * 根据身份证号获取性别 * * @param pid * 身份证号 * @return 性别 F为女M为男 */ public static String getSexByPid(Strin ...

  8. 全局axios默认值 和 自定义实例默认值

    首先说了一下情况, 登录后成功返回token 然后在带着token去继续下面的请求, 奇怪的是都是当前页面起作用,刷新和跳转之后就token 就消失了. 查了 axios文档发现 被自己坑了 我设置了 ...

  9. JVM指令的使用深入详解

    原文地址:https://www.jb51.net/article/155293.htm 一.未归类系列A 此系列暂未归类. 指令码    助记符                            ...

  10. Python学习笔记 使用数据库SQlite Mysql

    SQLite是一种嵌入式数据库,它的数据库就是一个文件.由于SQLite本身是C写的,而且体积很小,所以,经常被集成到各种应用当中, 甚至在IOS和Android的APP中都可以集成 Python就内 ...