E - Necklace
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 beautifulrelative 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.
Example
3
4 2 1
1
abacaba
1
4
4
aaaa
2
1 1
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,再给出n个整数a[i],分别表示颜色为i的珠子有a[i]个。求构造一串项链,使项链从某处剖开后对称,并使能剖开处的位置最多。输出时,先输出一个整数表示可剖开处的个数,然后输出
项链。相应的数字用对应的字母表示,如第一种颜色对a,第二种颜色对b...。
那么,由于我们需要尽可能多的"剖开处",这就意味着剖开后再拼接,字符串的构造与原来一样,这种情况要尽可能的多.
继续探究(下图):
在上图中,a,b的接口就是剖开处,显然a必定是b的前缀,也是b的后缀,至于中间部分,那还是可以继续分下去,然后又进行剖割......
我们可以假设一下,a是最小的剖割单位(即子串a无法继续剖割),那由于字符串是我们来构造的,那么一个贪心的想法,子串b中部必定有若干个子串a(这样,这个字符串才满足要求)
那么,我们就可以将目标串划分为若干个子串,这些子串是回文的,且不能再划分.
那到底有几个子串?我们要分类讨论.
首先,有可能根本构不成这样的目标串.什么时候?a[i]中存在两个及以上的奇数.这样的话,在每一个相同的子串中,你无法确定,正中间放谁.
另外,如果只有1个奇数,那么这个字符一定在每一个子串的中间.那究竟最多可以划分多少个子串?gcd(a[i])个.(这个应该不用证明了吧)
而且能保证,每个子串中,某个字符出现1次的有且只有1个.(这个也不用证了吧,水一水就好了)
然后回文输出就行.
如果根本没有奇数,那怎么办?
最多能划分成gcd(a[i])/2个子串(注意并不是gcd(a[i])).为什么?我们用反证法.假设能分成gcd(a[i])个子串,那么每个子串里面包含出现奇数次的字符的个数有可能不止一个,如果真的不止一个,那就不满足要求了.
然后还是分布在两边回文输出.
1 #include<cstdio>
2 #include<cstring>
3 #include<algorithm>
4 #include<iostream>
5 using namespace std;
6 int n,a[30],odd;
7 int gcd(int x,int y){return y==0?x:gcd(y,x%y);}
8 int main(){
9 cin>>n,odd=0;
10 for (int i=1; i<=n; i++) cin>>a[i],odd+=a[i]%2;
11 if (odd>1){
12 puts("0");
13 for (int i=1; i<=n; i++)
14 for (int j=1; j<=a[i]; j++) printf("%c",(char)(i-1+(int)'a'));
15 return 0;
16 }
17 int G=a[1];
18 for (int i=2; i<=n; i++) G=gcd(G,a[i]);
19 printf("%d\n",G);
20 int len=0;
21 for (int i=1; i<=n; i++) a[i]/=G,len+=a[i];
22 if (odd==1){
23 for (int t=1; t<=G; t++){
24 for (int i=1; i<=n; i++) if (a[i]%2==0)
25 for (int j=1; j<=a[i]/2; j++) printf("%c",(char)(i-1+(int)'a'));
26 for (int i=1; i<=n; i++) if (a[i]%2==1)
27 for (int j=1; j<=a[i]; j++) printf("%c",(char)(i-1+(int)'a'));
28 for (int i=n; i>=1; i--) if (a[i]%2==0)
29 for (int j=1; j<=a[i]/2; j++) printf("%c",(char)(i-1+(int)'a'));
30 }
31 }else{
32 for (int t=1; t<=G/2; t++){
33 for (int i=1; i<=n; i++)
34 for (int j=1; j<=a[i]; j++) printf("%c",(char)(i-1+(int)'a'));
35 for (int i=n; i>=1; i--)
36 for (int j=1; j<=a[i]; j++) printf("%c",(char)(i-1+(int)'a'));
37 }
38 }
39 return 0;
40 }