#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. FAQ: SNMP on NetScaler Appliance

    FAQ: SNMP on NetScaler Appliance https://support.citrix.com/article/CTX122436 https://docs.citrix.co ...

  2. 周记【距gdoi:117天】

    国庆被“吞”了 图论还剩下平面图.分层图.欧拉图…… 是现实太残酷还是自己兴趣不够? 努力吧.

  3. 洛谷P4593 [TJOI2018]教科书般的亵渎 【数学】

    题目链接 洛谷P4593 题解 orz dalao upd:经典的自然数幂和,伯努利数裸题 由题我们只需模拟出代价,只需使用\(S(n,k) = \sum\limits_{i = 1}^{n} i^{ ...

  4. AOJ.502 不只是水仙花

    不只是水仙花 Time Limit: 1000 ms Case Time Limit: 1000 ms Memory Limit: 64 MB Total Submission: 1196 Submi ...

  5. Codeforces Round #525 (Div. 2) F. Ehab and a weird weight formula

    F. Ehab and a weird weight formula 题目链接:https://codeforces.com/contest/1088/problem/F 题意: 给出一颗点有权值的树 ...

  6. ambari server内存溢出

    抛出的异常信息如下: java.lang.OutOfMemoryError: PermGen space at java.lang.ClassLoader.defineClass1(Native Me ...

  7. 停止ambari上服务的顺序

    Before performing any upgrades or uninstalling software, stop all of the Hadoop services in the foll ...

  8. Ecplise添加XML自动提示

    这里以struts.xml为例 第一步: 首先找到 struts2的核心jar包,我这里是struts2-core-2.3.20.jar用压缩工具打开或者解压下来

  9. Intelij idea新窗口打开项目设置

    1.idea 打开后 file->setting   2.appearance&behave->system setting->open project in new win ...

  10. 【BZOJ3132】上帝造题的七分钟 [树状数组]

    上帝造题的七分钟 Time Limit: 20 Sec  Memory Limit: 128 MB[Submit][Status][Discuss] Description “第一分钟,X说,要有矩阵 ...