Codeforces 614E - Necklace

614E - Necklace

思路:如果奇数超过1个,那么答案是0;否则,所有数的gcd就是答案。

构造方案:每个数都除以gcd,如果奇数个仍旧不超过1个,找奇数个那个在中间(如果没有奇数默认a),其他的平均分到两边。

如果奇数个数超过1个,为了保证中间点之间的每个字母个数是偶数个,那么就拿上种情况的两个构造一段(两个对称拼成一段)。

代码

#include<bits/stdc++.h>
using namespace std;
int a[26];
const int N=2e6;
char s[N];
int gcd(int a,int b)
{
    return b?gcd(b,a%b):a;
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n;
    cin>>n;
    int cnt=0;
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
        if(a[i]&1)cnt++;
    }
    if(n==1)
    {
        cout<<a[0]<<endl;
        for(int i=0;i<a[0];i++)
        cout<<‘a‘;
        cout<<endl;
        return 0;
    }
    if(cnt>=2)
    {
        cout<<0<<endl;
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<a[i];j++)cout<<(char)(i+‘a‘);
        }
        cout<<endl;
        return 0;
    }
    int ans=gcd(a[0],a[1]);
    for(int i=2;i<n;i++)
    ans=gcd(ans,a[i]);
    int l=1e6,r=1e6+1;
    int index=0;
    for(int i=0;i<n;i++)
    {
        a[i]/=ans;
        if(a[i]&1)index=i;
    }
    cnt=0;
    for(int i=0;i<n;i++)
    {
        if(a[i]&1)cnt++;
    }
    for(int i=0;i<a[index];i++)
    {
        s[r++]=‘a‘+index;
        //cout<<s[r-1]<<endl;
    }
    bool flag=true;
    for(int i=0;i<n;i++)
    {
        if(i==index)continue;
        while(a[i]!=0)
        if(flag)
        {
            s[l--]=‘a‘+i;
            a[i]--;
            flag=false;
        }
        else
        {
            s[r++]=‘a‘+i;
            a[i]--;
            flag=true;
        }
    }
    string as;
    for(int i=l+1;i<r;i++)as+=s[i];
    if(cnt>=2)for(int i=r-1;i>l;i--)as+=s[i];
    cout<<ans<<endl;
    if(cnt>=2)ans/=2;
    for(int i=0;i<ans;i++)
    cout<<as;
    cout<<endl;
    return 0;
}
时间: 2024-08-07 14:56:04

Codeforces 614E - Necklace的相关文章

寒假第二周 1.18 --- 1.24

1.18 cf 581c 581C - Developing Skills 重新自己写了一遍,注意都是0 的时候 1 #include<cstdio> 2 #include<cstring> 3 #include<iostream> 4 #include<algorithm> 5 #include<vector> 6 using namespace std; 7 8 const int maxn = 1e5+5; 9 int n,k; 10 11

codeforces 526 d Om Nom and Necklace next数组的灵活运用

codeforces 526 d Om Nom and Necklace 题意: 给出一个字符串,问对于字符串的每个位置p,求从0到p的字符串是否符合格式:S=A+B+A+B+A+...+A+B+A,其中A,B是字符串,且可以是空串. 限制: 字符串长度1e6 思路: next数组的灵活运用. /*codeforces 526 d Om Nom and Necklace 题意: 给出一个字符串,问对于字符串的每个位置p,求从0到p的字符串是否符合格式:S=A+B+A+B+A+...+A+B+A,

Codeforces 526D - Om Nom and Necklace 【KMP】

ZeptoLab Code Rush 2015 D. Om Nom and Necklace [题意] 给出一个字符串s,判断其各个前缀是否是 ABABA…ABA的形式(A和B都可以为空,且A有Q+1个,B有Q个,Q给定). [官方题解] 对于前缀P,我们可以把它拆成P=SSSS…SSSST,其中T是S的前缀.显然可以用KMP算法,时间复杂度是O(n). 当T=S:P=SSS…S.假设S出现了R次.如果转换为ABABAB…ABABA的形式,A和B是由几个S组成,而且最后的A一定是P的一个后缀.由

Codeforces 526D Om Nom and Necklace (KMP)

http://codeforces.com/problemset/problem/526/D 题意 给定一个串 T,对它的每一个前缀能否写成 A+B+A+B+...+B+A+B+A+B+...+B+A 的形式(k +1个 A,k 个 B,均可为空串) 分析 官方题解 对于前缀P,我们可以把它拆成P=SSSS…SSSST,其中T是S的前缀.显然可以用KMP算法,时间复杂度是O(n). 当T=S:P=SSS…S.假设S出现了R次.如果转换为ABABAB…ABABA的形式,A和B是由几个S组成,而且最

Codeforces 526D Om Nom and Necklace kmp+hash

题目链接:点击打开链接 题意: 给出长度为n的字符串,常数k 下面一个长度为n的字符串. 问: for(int i = 1; i <= n; i++){ 字符串的前i个字符 能否构成 形如A+B+A+B+A+B+A的形式,其中A有k+1个,B有k个 A和B是2个任意的字符串(也可以为空串) 若可以构成则输出1,否则输出0 } 思路: POJ1961 先用kmp求一个前缀循环节,. 我们观察 ABABABA => AB, AB, AB, A 所以前缀循环节有K个,而后面的A是尽可能地和AB长度接

Codeforces ZeptoLab Code Rush 2015

比赛链接:http://codeforces.com/contest/526/ A. King of Thieves time limit per test:1 second memory limit per test:256 megabytes In this problem you will meet the simplified model of game King of Thieves. In a new ZeptoLab game called "King of Thieves&quo

【codeforces 718E】E. Matvey&#39;s Birthday

题目大意&链接: http://codeforces.com/problemset/problem/718/E 给一个长为n(n<=100 000)的只包含‘a’~‘h’8个字符的字符串s.两个位置i,j(i!=j)存在一条边,当且仅当|i-j|==1或s[i]==s[j].求这个无向图的直径,以及直径数量. 题解:  命题1:任意位置之间距离不会大于15. 证明:对于任意两个位置i,j之间,其所经过每种字符不会超过2个(因为相同字符会连边),所以i,j经过节点至多为16,也就意味着边数至多

Codeforces 124A - The number of positions

题目链接:http://codeforces.com/problemset/problem/124/A Petr stands in line of n people, but he doesn't know exactly which position he occupies. He can say that there are no less than a people standing in front of him and no more than b people standing b

Codeforces 841D Leha and another game about graph - 差分

Leha plays a computer game, where is on each level is given a connected graph with n vertices and m edges. Graph can contain multiple edges, but can not contain self loops. Each vertex has an integer di, which can be equal to 0, 1 or  - 1. To pass th