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)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)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,0071,000,000,007.
Sample Input
3
aa
aabb
a
Sample Output
1
2
1
题意:
给你遗传由若干个小写字母组成的字符串,你可以任意变动互相之间的位子,但不能删除或添加元素,问可以组成多少种不同回文串。
因为结果都是回文串,对称,只要处理左边部分就好,所以就是求左边的排列组合,然后用逆元球结果就好
组合为C(左边剩下位置,当前字符的一半的个数)
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <stack>
#include <vector>
using namespace std;
typedef long long ll;
const int maxn=1e5+7;
const int mod=1e9+7;
ll al[30];
ll fact[maxn];
ll inv[maxn];
ll quickM(ll a, ll b){
ll base = a%mod, ans = 1;
while(b){
if(b&1) ans = ans*base%mod;
base = base*base%mod;
b >>= 1;
}
return ans;
}
void init(){
fact[0] = 1;
for(int i = 1; i <= maxn; ++i)
fact[i] = fact[i-1]*i%mod;
inv[maxn]=quickM(fact[maxn],mod-2);
for(int i=maxn-1;i>=0;i--)
{
inv[i]=inv[i+1]*(i+1);
inv[i]%=mod;
}
}
ll C(int n, int m){
return ((fact[n]*inv[m])%mod*(inv[n-m]))%mod;
}
int main()
{
int n;
init();
while(cin>>n)
{
while(n--)
{
memset(al,0,sizeof(al));
string s;
cin>>s;
int len=s.length();
sort(s.begin(),s.end());
for(int i=0;i<len;i++){
al[s[i]-'a']++;
}
int cnt=0;
for(int i=0;i<26;i++)
{
if(al[i]%2)
{
cnt++;
}
}
if(cnt>1)
{
printf("0\n");
continue;
}
ll cha=len/2;
ll sum=1;
for(int i=0;i<26;i++)
{
if(al[i]>0)
{
sum*=C(cha,al[i]/2);
cha-=al[i]/2;
sum%=mod;
}
}
cout<<sum<<endl;
}
}
}