#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. [Leetcode] Remove duplicates from sorted array 从已排序的数组中删除重复元素

    Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...

  2. oracle大数据匹配处理C#

    忙碌了几天写出来的oracle存储过程在作业中执行. 写的oracle存储过程如果有什么不好的地方大家指点指点. oracle存储过程其中使用到游标嵌套.if.if嵌套.数据插入表.select插入表 ...

  3. codeforces 1060 B

    https://codeforces.com/contest/1060/problem/B 题意:给你一个数C ,你要找到两个数A.B,使得A+B=C并且A的每个位的数的和最大,求最大的和是多少 题解 ...

  4. 用@Component注解代替@Configuration注解,定义bean

    package com.timo.entity; import org.springframework.beans.factory.annotation.Value; import org.sprin ...

  5. nginx对指定目录做代理

    环境介绍 web1,作为前端端服务器,访问地址是http://192.168.1.1,要将http://192.168.1.1/bbs的请求交给web2.在web1的网站根目录下并没有bbs目录 we ...

  6. .NET的PE文件结构篇(转)

    一.开篇 开篇我要讲述一个关于PE文件结构的文章,这篇文章动手能力比较强,希望大家能够动手进行操作,这边文章篇幅有可能会长一些,为了方便大家阅读我可以将其分为几个部分进行讲解,主要分为以下几个部分: ...

  7. MySql数据库学习总结(MySQL入门到精通)

    2017.1.24-2.3日(在大兴实验室) 1.数据库存储引擎: (1)MyISAM: 访问速度快,对事物完整性没要求,并以访问为主的适合这个 (2)InnoDB: 更占磁盘空间,需要进行频繁的更新 ...

  8. NYOJ 349 Sorting It All Out (拓扑排序 )

    题目链接 描述 An ascending sorted sequence of distinct values is one in which some form of a less-than ope ...

  9. bzoj 2705 数学 欧拉函数

    首先因为N很大,我们几乎不能筛任何东西 那么考虑设s(p)为 gcd(i,n)=p 的个数,显然p|n的时候才有意义 因为i与n的gcd肯定是n的因数,所以那么可得ans=Σ(p*s(p)) 那么对于 ...

  10. selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH.

    http://www.seleniumhq.org/download   1. selenium 3.x开始,webdriver/firefox/webdriver.py的__init__中,exec ...