杭电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): 1358    Accepted Submission(s): 387


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
 

Source
 

Recommend
wange2014   |   We have carefully selected several similar problems for you:   5654  5653  5652  5649  5648 


挺好的一道题,来讲个规律吧,(a+b+c+d+...z)的阶乘==c(a+b+c....+y+z,a+b+c+d+...y)*c(a+b+c+d+......+x+y,a+b+c+d+....+x),*....c(a+b,a);

根据(a+b)!=c(a+b,a)推出来的,那么题目可以转换成,已知有m个字母,每个字母有不同的个数,问排列起来有多少种排列方式,就用到了上边的公式,因为题目所用的数据比较大,所以求组合数的函数不能用,需要用到杨辉三角,杨辉三角和组合数的过程是一样的,那么便得到了答案:

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#include<map>
using namespace std;
char s[110000];
char c[11000];
__int64 i,j,k,l,m,n,p,biaoji[1100],ch[2010][2010];
//__int64 ac(__int64 m,__int64 n)//组合函数不对,因为当ans大到一定程度的时候 
//{//有一个取余的过程,这个过程可能造成ans/i不为整数 
//	__int64 ans=1;
//	__int64 k=m;
//	//n=min(n,m-n);
//	for(__int64 i=1;i<=n;i++)
//	{
//		ans*=(k-i+1);
//		ans/=i;
//		ans%=1000000007;
//	}
//	return ans;
//} 
int main()
{
	memset(ch,0,sizeof(ch));
    ch[0][0]=1;//需要用杨辉三角来代替求组合数的函数 
    for( i=1; i<=510; i++)
    {
        ch[i][0]=1;
        for( j=1; j<=510; j++)
        {
            ch[i][j]=(ch[i-1][j]+ch[i-1][j-1])%1000000007;
        }
    }
	scanf("%I64d",&p);
	while(p--)
	{
		scanf("%s",s);
		map<char , int> q;//map求出每个字母对应的个数 
		l=strlen(s);
		q.clear();
		int cnt=0;
		for(i=0;i<l;i++)
		{
				if(!q[s[i]])
				{
					c[cnt]=s[i];
					cnt++;
					q[s[i]]++;
				}
				else
				q[s[i]]++;
		}
		int cmt=0;
		for(i=0;i<cnt;i++)
		{
			if(q[c[i]]%2)
			{
				cmt++;
				q[c[i]]--;
			}
		}
		if(cmt>1)
		printf("0\n");
		else
		{
			int nn=q[c[0]]/2;
			__int64 ans=1;
			for(i=1;i<cnt;i++)
			{
				//ans*=ac(nn+q[c[i]]/2,nn);
				ans*=ch[nn+q[c[i]]/2][nn];
				ans%=1000000007;
				nn+=q[c[i]]/2;
				//printf("%I64d\n",ac(nn+q[c[i]]/2,nn));
			}
			printf("%I64d\n",ans);
		}
	//	printf("%I64d\n",ac(44,42));
	//	printf("%I64d\n",ac(44,2));
	//	printf("%I64d\n",ac(42,21));
	}
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值