HDU 5651

来源:互联网 发布:51单片机编程软件 编辑:程序博客网 时间:2024/06/06 04:27
 xiaoxin juju needs help
Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
Submit Status

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  which represents the number of test cases. 
For each test case, there is a single line containing a string 

Output

For each test case, print an integer which is the number of watermelon candies xiaoxin's leader needs to buy after mod 

Sample Input

3aaaabba

Sample Output

121


题意,给你一个字符串求回文组合数,利用回文的特性判断一半就可以然后运用组合数的知识

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
const int N = 600;
char str[2*N];
int visit[N];
int c[N][N];
const int  mod = 1000000007;


int main()
{
    memset(c,0,sizeof(c));
    for(int i=0;i<=N;i++)
    {
        c[i][0]=c[i][i]=1;
    }
    for(int i=2;i<=N;i++)
    {
        for(int j=1;j<i;j++)
        {
            c[i][j]=(c[i-1][j-1]%mod+c[i-1][j]%mod)%mod;
        }
    }
    int t;
    scanf("%d", &t);
    while(t--)
    {
        scanf("%s", str);
        int len=strlen(str);
        memset(visit,0,sizeof(visit));
        for(int i=0;str[i];i++)
        {
            int v=str[i]-'a';
            visit[v]++;
        }
        int num=0;
        for(int i=0;i<26;i++)
        {
            if(visit[i]&1)
            {
                num++;
            }
        }
        if(num>1)
        {
            printf("0\n");
            continue;
        }
        else
        {
            len/=2;
            long long sum=1;
            for(int i=0;i<26;i++)
            {
                visit[i]/=2;
                if(visit[i])
                {
                    int w=visit[i];
                    sum=((sum%mod)*(c[len][w]%mod))%mod;
                    len-=w;
                }
            }
            printf("%I64d\n",sum);
        }
    }
    return 0;
}


0 0
原创粉丝点击