Kolya has a string s of length n consisting of lowercase and uppercase Latin letters and digits.
He wants to rearrange the symbols in s and cut it into the minimum number of parts so that each part is a palindrome and all parts have the same lengths. A palindrome is a string which reads the same backward as forward, such as madam or racecar.
Your task is to help Kolya and determine the minimum number of palindromes of equal lengths to cut s into, if it is allowed to rearrange letters in s before cuttings.
Input
The first line contains an integer n (1 ≤ n ≤ 4·10^5) — the length of string s.
The second line contains a string s of length n consisting of lowercase and uppercase Latin letters and digits.
Output
Print to the first line an integer k — minimum number of palindromes into which you can cut a given string.
Print to the second line k strings — the palindromes themselves. Separate them by a space. You are allowed to print palindromes in arbitrary order. All of them should have the same length.
Examples
input
6
aabaac
output
2
aba aca
input
8
0rTrT022
output
1
02TrrT20
input
2
aA
output
2
a A
大致题意:给你一串长度为n的由大小写字母和0~9数字组成的字符串。让你重新组合排序分成若干个长度相同的回文子串,问子串最少的数量,然后输出。
思路:首先,我们可以记录下不同种类的字符的数量分别是多少,如果每一种类的字符数量都是偶数个,那么我们就可以将其重新排序使得其成为一个回文串,假设有x种字符的数量为奇数个,那么我们需要将这x个字符分别都提取一个出来,放置在不同子串的中心位置,所以至少需要x个子串,然后每个子串的长度都得相同且长度是奇数,假如n能被x整除且所得值是奇数,说明此时能满足,否则,我们需要提取2个相同的字符,然后新增两个子串,放置在其中间位置,重复该过程,直到满足n能被x+2*k整除,且所得值为奇数,k为提取的次数。然后暴力去搞就行了。
代码写的很挫,以后有时间再来优化下。。。。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=400005;
char str[maxn];
int s1[10];//num
int s2[26];//daxie
int s3[26];//xiaoxie
char q[maxn];//存放每个子串中间的那个字符,注意数组得开大点,所放字符可能重复
int main()
{
int n;
scanf("%d",&n);
scanf("%s",str);
for(int i=0;i<n;i++)
{
if(str[i]>='a'&&str[i]<='z')
{
s3[str[i]-'a']++;
}
else if(str[i]>='A'&&str[i]<='Z')
{
s2[str[i]-'A']++;
}
else
{
s1[str[i]-'0']++;
}
}
int num=0;//记录数量为奇数的字符种类个数
for(int i=0;i<26;i++)
{
if(s2[i]%2==1)
{
char c='A'+i;
q[num]=c;
s2[i]--;
num++;
}
if(s3[i]%2==1)
{
char c='a'+i;
q[num]=c;
s3[i]--;
num++;
}
}
for(int i=0;i<10;i++)
if(s1[i]%2==1)
{
char c='0'+i;
q[num]=c;
s1[i]--;
num++;
}
if(num==0)
{
printf("1\n");
char str2[maxn];
int f=0;
for(int i=0;i<10;i++)
{
while(s1[i]>0)
{
char c='0'+i;
str2[f]=c;
str2[n-1-f]=c;
s1[i]-=2;
f++;
}
}
for(int i=0;i<26;i++)
{
while(s2[i]>0)
{
char c='A'+i;
str2[f]=c;
str2[n-1-f]=c;
s2[i]-=2;
f++;
}
}
for(int i=0;i<26;i++)
{
while(s3[i]>0)
{
char c='a'+i;
str2[f]=c;
str2[n-1-f]=c;
s3[i]-=2;
f++;
}
}
for(int i=0;i<n;i++)
printf("%c",str2[i]);
}
else
{
int l=0;//提取次数
while(1)
{
if(n%(num+2*l)==0)
if(n/(num+2*l)%2==1)
break;
l++;
}
printf("%d\n",num+2*l);
int len=n/(num+2*l)-1;
if(len==0)//全部都分成n个子串
{
for(int i=0;i<n;i++)
printf("%c ",str[i]);
return 0;
}
char str2[maxn];
int f=0;
str2[len/2]=q[f];
f++;
int k=0;
for(int i=0;i<26;i++)
{
while(s2[i]>0&&l)
{
char c='A'+i;
q[num++]=c;
q[num++]=c;
s2[i]-=2;
l--;
}
while(s3[i]>0&&l)
{
char c='a'+i;
q[num++]=c;
q[num++]=c;
s3[i]-=2;
l--;
}
}
for(int i=0;i<10;i++)
{
while(s1[i]>0&&l)
{
char c='0'+i;
q[num++]=c;
q[num++]=c;
s1[i]-=2;
l--;
}
}
num++;
for(int i=0;i<10;i++)
{
while(s1[i]>0)
{
char c='0'+i;
str2[k]=c;
str2[len-k]=c;
s1[i]-=2;
k++;
if(k==len/2)
{
for(int j=0;j<=len;j++)
printf("%c",str2[j]);
printf(" ");
str2[len/2]=q[f];
f++;
if(f==num) return 0;
k=0;
}
}
}
for(int i=0;i<26;i++)
{
while(s2[i]>0)
{
char c='A'+i;
str2[k]=c;
str2[len-k]=c;
s2[i]-=2;
k++;
if(k==len/2)
{
for(int j=0;j<=len;j++)
printf("%c",str2[j]);
printf(" ");
str2[len/2]=q[f];
f++;
if(f==num) return 0;
k=0;
}
}
}
for(int i=0;i<26;i++)
{
while(s3[i]>0)
{
char c='a'+i;
str2[k]=c;
str2[len-k]=c;
s3[i]-=2;
k++;
if(k==len/2)
{
for(int j=0;j<=len;j++)
printf("%c",str2[j]);
printf(" ");
str2[len/2]=q[f];
f++;
if(f==num) return 0;
k=0;
}
}
}
}
return 0;
}