codeforces 613c Necklace

C. Necklace
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 npositive 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.

Examples
input
Copy
3
4 2 1
output
1
abacaba
input
Copy
1
4
output
4
aaaa
input
Copy
2
1 1
output
0
ab
Note

In the first sample a necklace can have at most one beautiful cut. The example of such a necklace is shown on the picture.

In the second sample there is only one way to compose a necklace.

题意:  给你n个颜色 和每个颜色的珠子的个数   你要组成一个环  这个环 如果从某一个地方剪开的话是个  回文串,  那么这个环的魅力值就加一

你要求出最大的魅力值  和  这个环的模样

思路:  如果有两种颜色的珠子个数是奇数个  那么魅力值就一定是0  那么任意输出一个串就可以。

再其次  我们可以想到每个颜色的珠子要平均分到 剪开后的字符串中,这样 才可以保证剪开后为回文字符串。 

所以魅力值一定是颜色个数的最大公约数。

再其次我们就要考虑怎么输出。   我们将cnt[i]/=num   ,那么cnt就是每个小字符串应该包含的颜色的个数。

如果其中有两个或以上的颜色个数为奇数 , 那么就一定是要将两个小字符串组成一个大点的字符串输出。(自己画一画)

然后颜色个数为奇数的个数 小于2 的情况就可以直接对称着输出就可以。

代码: 

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#define lalala printf("lalala\n");
#define N 35

using namespace std;

int n,cnt[N];
int c[N];

int main()
{
	cin>>n;
	for(int i=1;i<=n;i++) cin>>cnt[i];
	int num=cnt[1];
	
	int flag=0;
	for(int i=1;i<=n;i++)
	{
		if(cnt[i]&1)
		{
			flag++;
			if(flag==2) break;
		}
	}
	
	for(int i=2;i<=n;i++) num=__gcd(num,cnt[i]);
	
	if(flag==2)
	{
		printf("0\n");
		for(int i=1;i<=n;i++){
			if(cnt[i])
			{
				while(cnt[i]--) printf("%c",i+'a'-1);
			}
		}
		printf("\n");
		return 0;
	}
	
	for(int i=1;i<=n;i++) cnt[i]/=num;
	
	/*
	for(int i=1;i<=n;i++) printf("%d ",cnt[i]);
	printf("\n");
	*/
	
	
	printf("%d\n",num);
	
	flag=0;
	for(int i=1;i<=n;i++){
		if(cnt[i]%2==1){
			flag++;
			if(flag==2) break;
		}
	}
	
	if(flag==2)
	{
		num/=2;
		while(num--)
		{
		memcpy(c,cnt,sizeof(cnt));
		for(int i=1;i<=n;i++)
		{
			while(c[i])
			{
				printf("%c",'a'+i-1);
				c[i]--;
			}
		}
		memcpy(c,cnt,sizeof(cnt));
		for(int i=n;i>=1;i--)
		{
			while(c[i]){
				printf("%c",'a'+i-1);
				c[i]--;
			}
		}
	   
	    }
	    cout<<endl;
		
	}
	else
	{
		//lalala
		while(num--)
		{
			memcpy(c,cnt,sizeof(cnt));
			for(int i=1;i<=n;i++)
			{
				if(c[i]%2==0)
				{
					c[i]/=2;
					while(c[i]){
						printf("%c",'a'+i-1);
						c[i]--;
					}
				}
			}
			for(int i=1;i<=n;i++){
				if(c[i]%2)
				{
					while(c[i]){
						printf("%c",'a'+i-1);
						c[i]--;
					}
				}
			}
			memcpy(c,cnt,sizeof(c));
			for(int i=n;i>=1;i--)
			{
				if(c[i]%2==0)
				{
					c[i]/=2;
					while(c[i]){
						printf("%c",'a'+i-1);
						c[i]--;
					}
				}
			}
			
		}
		printf("\n");
	}
	return 0;
} 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值