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.
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.
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.
3 4 2 1
1 abacaba
1 4
4 aaaa
2 1 1
0 ab
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;
}