Codeforces Round #339 (Div. 1) C. Necklace 构造题

本文针对Codeforces上的一道算法题进行解析,该题要求构造一个由不同颜色珠子组成的最美丽项链,使其在尽可能多的位置切割后仍能形成回文串。文章详细介绍了问题背景、解决方案及特殊情形处理。

C. Necklace

题目连接:

http://www.codeforces.com/contest/613/problem/C

Description

Ivan wants to make a necklace as a present to his beloved girl. A necklace is a cyclic sequence of beads of different colors. Ivan says that necklace is beautiful relative to the cut point between two adjacent beads, if the chain of beads remaining after this cut is a palindrome (reads the same forward and backward).

Ivan has beads of n colors. He wants to make a necklace, such that it's beautiful relative to as many cuts as possible. He certainly wants to use all the beads. Help him to make the most beautiful necklace.

Input

The first line of the input contains a single number n (1 ≤ n ≤ 26) — the number of colors of beads. The second line contains after n positive integers ai — the quantity of beads of i-th color. It is guaranteed that the sum of ai is at least 2 and does not exceed 100 000.

Output

In the first line print a single number — the maximum number of beautiful cuts that a necklace composed from given beads may have. In the second line print any example of such necklace.

Each color of the beads should be represented by the corresponding lowercase English letter (starting with a). As the necklace is cyclic, print it starting from any point.

Sample Input

3
4 2 1

Sample Output

1
abacaba

Hint

题意

你有n个颜色的珠子,你要把所有珠子都穿成一个圆环

然后你可以从圆环的某一个地方剪开,使得这个链是一个回文串

然后问你怎么样去构造那个圆环,可以使得构成回文串的链最多

题解

考虑对称性,如果答案是ans的话,那么这ans刀一定是等分这个圆环的

每一个块内的珠子数量肯定是相同的

那么显然要满足这两个特性,ans = gcd(a[i])

有两个特殊情况需要考虑

如果存在大于1个颜色的珠子为奇数的话,答案为0,显然

如果有一个颜色的珠子为奇数的话,只要把这个珠子放在中间就好了,相当于把其他的偶数都砍一半,然后去摆放。

代码

#include<bits/stdc++.h>
using namespace std;

int p[30];
int odd = 0;
int gcd(int a,int b)
{
    if(b==0)return a;
    return gcd(b,a%b);
}
int main()
{
    int n;scanf("%d",&n);
    int t = 0;
    for(int i=0;i<n;i++)
    {
        scanf("%d",&p[i]);
        if(p[i]%2==1)t=i,odd++;
    }
    if(odd>1)
    {
        printf("0\n");
        for(int i=0;i<n;i++)
            for(int j=0;j<p[i];j++)
                printf("%c",'a'+i);
        printf("\n");
        return 0;
    }
    if(odd==1)
    {
        int ans = 0;
        for(int i=0;i<n;i++)
        {
            if(i==t)ans=gcd(p[i],ans);
            else ans=gcd(p[i]/2,ans);
        }
        printf("%d\n",ans);
        for(int i=0;i<ans;i++)
        {
            for(int j=0;j<n;j++)
                for(int k=0;k<p[j]/ans/2;k++)
                    printf("%c",'a'+j);
            printf("%c",'a'+t);
            for(int j=n-1;j>=0;j--)
                for(int k=0;k<p[j]/ans/2;k++)
                    printf("%c",'a'+j);
        }
    }
    else
    {
        int ans = 0;
        for(int i=0;i<n;i++)
            ans=gcd(p[i],ans);
        printf("%d\n",ans);
        for(int i=0;i<ans/2;i++)
        {
            for(int j=0;j<n;j++)
                for(int k=0;k<p[j]/ans;k++)
                    printf("%c",'a'+j);
            for(int j=n-1;j>=0;j--)
                for(int k=0;k<p[j]/ans;k++)
                    printf("%c",'a'+j);
        }
    }
}

转载于:https://www.cnblogs.com/qscqesze/p/5217571.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值