Codeforces Round #445 div.2 D. Restoration of string 乱搞

D. Restoration of string

题意:给你n个字符串,让你构造一个终串,使得这n个字符串都是终串的最小频繁子串,如果不存在输出NO。  最频繁子串:出现次数最多的子串

tags: 直接暴力怼??

#include<bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
#define rep(i,a,b) for (int i=a; i<=b; ++i)
#define per(i,b,a) for (int i=b; i>=a; --i)
#define mes(a,b)  memset(a,b,sizeof(a))
#define INF 0x3f3f3f3f
#define MP make_pair
#define PB push_back
#define fi  first
#define se  second
typedef long long ll;
const int N = 200005;

int n, in[28];
char s[N];
bool vis[28], used[28];
vector< int > G[N];
void print(int x)
{
    putchar(‘a‘+x);
    vis[x] = true;
    if(G[x].size()==1)
        print(G[x][0]);
}
bool check(int x)
{
    if(vis[x]) return false;
    vis[x]=true;
    if(G[x].size()==1)
        return check(G[x][0]);
    return true;
}
int main()
{
    scanf("%d", &n);
    rep(i,1,n)
    {
        scanf("%*c%s", s+1);
        mes(vis, false);
        for(int j=1; s[j]; ++j)
        {
            int id = s[j]-‘a‘;
            if(vis[id]) return 0*printf("NO\n");
            vis[id]=true, used[id]=true;
            if(j>1) {
                if(G[s[j-1]-‘a‘].size()==1 && G[s[j-1]-‘a‘][0]==s[j]-‘a‘)
                    continue;
                G[s[j-1]-‘a‘].PB(s[j]-‘a‘);
                ++in[s[j]-‘a‘];
            }
            if(G[s[j-1]-‘a‘].size()>1 || in[s[j]-‘a‘]>1)
                return 0*printf("NO\n");
        }
    }
    mes(vis, false);
    rep(i,0,25)
        if(used[i]) {
            mes(vis, false);
            if(!check(i)) return 0*printf("NO\n");
        }
    mes(vis, false);
    rep(i,0,25)
        if(used[i] && !vis[i] && in[i]==0)
            print(i);
    puts("");

    return 0;
}

原文地址:https://www.cnblogs.com/sbfhy/p/7832604.html

时间: 2024-07-30 11:51:46

Codeforces Round #445 div.2 D. Restoration of string 乱搞的相关文章

【CF886D】Restoration of string 乱搞

[CF886D]Restoration of string 题意:对于给定的一个母串,定义一个字符串是出现频率最多的,当且仅当它在母串中出现的次数最多(可以有多个出现次数最多的,出现的位置可以重叠). 现在给你一个字符串集合S,问你如果要求S中的所有字符串的出现频率都是最多的,最短的母串是什么.(如果有多个长度相同的母串,输出字典序最小的). |S|<=100000 题解:容易发现出现次数最多的一定是单个字符. 那么对于S中任意两个相邻的字符ab,一旦出现a后面就只能出现b.所以我们可以从a到b

Codeforces Round #354 (Div. 2) C. Vasya and String

题目大意:有一个(a|b)^N的由a和b构成的长度为N的字符串,允许修改其中k位.问能构成的最长的全为a或全为b的子串的长度最长为多少. 思路,用两个队列分别保存前K+1个a和b的位置,以i为结尾的最长的子串的长度就是i减去队列头元素的值. #include <iostream> #include <cstdio> #include <memory.h> #include <queue> using namespace std; int main(int a

Codeforces Round #297 (Div. 2)B Pasha and String

B. Pasha and String time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Pasha got a very beautiful string s for his birthday, the string consists of lowercase Latin letters. The letters in the

Codeforces Round #501 (Div. 3) B Obtaining the String

翻译 给你两个字符串\(s\)与\(t\),你每次可以交换字符串\(s\)种相邻两个字符,请你输出字符串\(s\)变成\(t\)的步骤(如果输出\(k\),代表交换了\(k\)与\(k+1\)),如果有多组解,随意输出一种即可. 思路 这道题一开始考虑复杂了,导致我发奋图强到\(11:40\)才\(A\)掉,我\(12:00\)必须睡觉因为明天有课\(www\). 实际不难,这题是\(SPJ\),我是这么想的:我们都知道任意\(1\)个字符可以通过交换相邻的两个字符来跑遍整个字符串. 进而可以得

【排序】【规律】Codeforces Round #254 (Div. 2) - D. Rooter&#39;s Song

D. DZY Loves FFT Source http://codeforces.com/contest/445/problem/D Description Wherever the destination is, whoever we meet, let's render this song together. On a Cartesian coordinate plane lies a rectangular stage of size w?×?h, represented by a re

Codeforces Round #428 (Div. 2)

Codeforces Round #428 (Div. 2) A    看懂题目意思就知道做了 #include<bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:102400000,102400000") #define rep(i,a,b) for (int i=a; i<=b; ++i) #define per(i,b,a) for (int i=b; i>=a; --i

Codeforces Round #424 (Div. 2) D. Office Keys(dp)

题目链接:Codeforces Round #424 (Div. 2) D. Office Keys 题意: 在一条轴上有n个人,和m个钥匙,门在s位置. 现在每个人走单位距离需要单位时间. 每个钥匙只能被一个人拿. 求全部的人拿到钥匙并且走到门的最短时间. 题解: 显然没有交叉的情况,因为如果交叉的话可能不是最优解. 然后考虑dp[i][j]表示第i个人拿了第j把钥匙,然后 dp[i][j]=max(val(i,j),min(dp[i-1][i-1~j]))   val(i,j)表示第i个人拿

Codeforces Round #424 (Div. 2) C. Jury Marks(乱搞)

题目链接:Codeforces Round #424 (Div. 2) C. Jury Marks 题意: 给你一个有n个数序列,现在让你确定一个x,使得x通过挨着加这个序列的每一个数能出现所有给出的k个数. 问合法的x有多少个.题目保证这k个数完全不同. 题解: 显然,要将这n个数求一下前缀和,并且排一下序,这样,能出现的数就可以表示为x+a,x+b,x+c了. 这里 x+a,x+b,x+c是递增的.这里我把这个序列叫做A序列 然后对于给出的k个数,我们也排一下序,这里我把它叫做B序列,如果我

[Codeforces] Round #352 (Div. 2)

人生不止眼前的狗血,还有远方的狗带 A题B题一如既往的丝帛题 A题题意:询问按照12345678910111213...的顺序排列下去第n(n<=10^3)个数是多少 题解:打表,输出 1 #include<bits/stdc++.h> 2 using namespace std; 3 int dig[10],A[1005]; 4 int main(){ 5 int aa=0; 6 for(int i=1;;i++){ 7 int x=i,dd=0; 8 while(x)dig[++dd