xiaoxin juju needs help
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1657 Accepted Submission(s): 499
Problem Description
As we all known, xiaoxin is a brilliant coder. He knew **palindromic** strings when he was only a six grade student at elementry school.
This summer he was working at Tencent as an intern. One day his leader came to ask xiaoxin for help. His leader gave him a string and he wanted xiaoxin to generate palindromic strings for him. Once xiaoxin generates a different palindromic string, his leader will give him a watermelon candy. The problem is how many candies xiaoxin's leader needs to buy?
Input
This problem has multi test cases. First line contains a single integer
T(T≤20) which represents the number of test cases.
For each test case, there is a single line containing a string
S(1≤length(S)≤1,000) .
Output
For each test case, print an integer which is the number of watermelon candies xiaoxin's leader needs to buy after mod
1,000,000,007 .
Sample Input
Sample Output
Source
BestCoder Round #77 (div.2)
Recommend
wange2014 | We have carefully selected several similar problems for you: 5679 5678 5677 5676 5674
刚开始看到这道题时下了一跳,后来仔细想想属于组合数学的知识,题意可以转换为有几种不同的字母,每种字母都有不同的数量,问有多少种不同的排列方式,不过有点无奈的是以前学的组合数学知识差不多忘完了。。。。。
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int a[510][510];
void zuo()
{
int i,j;
for(i=0;i<=500;i++)
{
a[i][0]=1;
}
for(i=0;i<=500;i++)
for(j=1;j<=i;j++)
{
a[i][j]=(a[i-1][j-1]+a[i-1][j])%1000000007;
}
}
int main()
{
int t,i;
char s[110000];
int num[30];
zuo();
scanf("%d",&t);
while(t--)
{
scanf("%s",s);
memset(num,0,sizeof(num));
int len=strlen(s);
for(i=0;i<len;i++)
{
num[s[i]-'a'+1]++;
}
int odd=0;
for(i=1;i<=26;i++)
{
if(num[i]%2!=0)
{
odd++;
num[i]=0;
}
else
num[i]=num[i]/2;
}
if(odd>1||(odd==1&&len%2==0))
{
printf("0\n");
continue;
}
__int64 ans=1;
len=len/2;
for(i=1;i<=26;i++)
{
ans=(ans*a[len][num[i]])%1000000007;
len=len-num[i];
}
printf("%I64d\n",ans);
}
return 0;
}