hdoj-5651-【xiaoxin juju needs help】

xiaoxin juju needs help

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1868    Accepted Submission(s): 564


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(T20)  which represents the number of test cases.
For each test case, there is a single line containing a string  S(1length(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
  
  
3 aa aabb a
 

Sample Output
  
  
1 2 1
 


解题思路很简单,主要是排列组合不知道怎么办,如果是回文串我们只需对回文串的一半
进行排列组合即可,但是在排列组合中有相同的元素,需要用到公式
比如有 n 个元素,1有a个,2有b个,3有c个有多少种排列方式
n!/(a!*b!*c!)  知道这些就简单了许多


<span style="font-size:18px;">#include<cstdio>
#include<cstring>
#define mod 1000000007
#define LL long long
LL num[1100],pre[1100];//num计算阶乘,pre存放逆元
LL counts[30]; 
char s[1200]; 
void init()
{
	int i,j;
	num[0]=num[1]=1; 
	for(i=2;i<1100;++i)
	{
		num[i]=i*num[i-1]%mod;
	}
}
LL quick_pow(LL a,LL n)
{
	LL ans=1;
	while(n)
	{
		if(n&1)
		{
			ans=ans*a%mod;
		} 
		a=a*a%mod;
		n>>=1;
	} 
	return ans;
} 
void get_pre()//得到逆元
{
	pre[0]=pre[1]=1; 
	for(int i=1;i<1100;++i)
	{
		pre[i]=quick_pow(num[i],mod-2); 
	} 
} 
int main()
{
	init();
	get_pre(); 
	int t;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%s",s);
		int len=strlen(s),i;
		memset(counts,0,sizeof(counts)); 
		for(i=0;i<len;++i)
		{
			 counts[s[i]-'a'+1]++; 
		}
		LL ant=0;
		for(i=1;i<=26;++i)
		{
			if(counts[i]&1)
			{
				++ant;//记录奇数个字母有多少个 
			}
			counts[i]>>=1; 
		}
		if(ant>1)
		{
			printf("0\n");
			continue; 
		}
		len/=2;
		LL a=num[len],b=1;
		for(i=1;i<=26;++i)
		{
			if(!counts[i]) 
				continue; 
			b=b*pre[counts[i]]%mod; 
		}
		b=(a%mod*b)%mod; 
		printf("%lld\n",b); 
	} 
	return 0;
} 
</span>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值