hdu 6034 Balala Power!(DP)



Balala Power!

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 998    Accepted Submission(s): 165


Problem Description

Talented  Mr.Tang has  n  strings consisting of only lower case characters. He wants to charge them with Balala Power (he could change each character ranged from  ato  z into each number ranged from  0 to  25, but each two different characters should not be changed into the same number) so that he could calculate the sum of these strings as integers in base  26  hilariously.

Mr.Tang wants you to maximize the summation. Notice that no string in this problem could have leading zeros except for string "0". It is guaranteed that at least one character does not appear at the beginning of any string.

The summation may be quite large, so you should output it in modulo  109+7 .
 

Input
The input contains multiple test cases.

For each test case, the first line contains one positive integers  n , the number of strings.  (1n100000)

Each of the next  n  lines contains a string  si  consisting of only lower case letters.  (1|si|100000,|si|106)
 

Output
For each test case, output " Case # x y " in one line (without quotes), where  x  indicates the case number starting from  1  and  y  denotes the answer of corresponding case.
 

Sample Input
      
      
1 a 2 aa bb 3 a ba abc
 

Sample Output
      
      
Case #1: 25 Case #2: 1323 Case #3: 18221
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<vector>
#include<map>
//#include <bits/stdc++.h>
using namespace std;
const int N = 1e6+10;;
typedef long long LL;
const LL mod = 1e9+7;
map<int,LL>q[30];
char str[N];
int vis[30], power[30];
double dp[100000+7][28];
vector<string>p;
int a[N];
struct node
{
    int id;
    LL cnt;
    double x;
    bool operator> (const node &A)
    {
        return x>A.x;
    }
}p1[30];
int cmp(node A,node B)
{
    return A.x>B.x;
}

int main()
{
    int ncase=1, n;
    while(scanf("%d", &n)!=EOF)
    {
        memset(vis,0,sizeof(vis));
        memset(dp,0,sizeof(dp));
        memset(power,0,sizeof(power));
        p.clear();
        int l=0;
        for(int t=0;t<n;t++)
        {
            scanf("%s",str);

            vis[str[0]-'a']=1;
            int len=strlen(str);
            a[t]=len;
            l=max(l,len);
            //reverse(str,str+len);
            //cout<<str<<endl;
            p.push_back(str);
            for(int i=0;i<len;i++)  dp[len-i-1][str[i]-'a']++;
        }
        for(int i=1;i<=l;i++)
            for(int j=0;j<26;j++) dp[i][j]+=(dp[i-1][j]/26.0);

        for(int i=0;i<26;i++)
        {
            p1[i].id=i,p1[i].x=dp[l][i];
        }
        sort(p1,p1+26,cmp);
        for(int i=0;i<26;i++)  power[p1[i].id]=25-i;
        int k=25;
        while(vis[p1[k].id])
        {
            swap(power[p1[k].id],power[p1[k-1].id]);
            k--;
        }
        LL sum=0;
        int m=p.size();
        for(int i=0;i<m;i++)
        {
            LL ans=0;
            for(int j=0;j<a[i];j++)
            {
                ans=(ans*26+power[p[i][j]-'a'])%mod;
            }
            sum=(sum+ans)%mod;
        }
        printf("Case #%d: %lld\n",ncase++,sum);
    }
    return 0;
}
























Balala Power!

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 998    Accepted Submission(s): 165


Problem Description

Talented  Mr.Tang has  n  strings consisting of only lower case characters. He wants to charge them with Balala Power (he could change each character ranged from  ato  z into each number ranged from  0 to  25, but each two different characters should not be changed into the same number) so that he could calculate the sum of these strings as integers in base  26  hilariously.

Mr.Tang wants you to maximize the summation. Notice that no string in this problem could have leading zeros except for string "0". It is guaranteed that at least one character does not appear at the beginning of any string.

The summation may be quite large, so you should output it in modulo  109+7 .
 

Input
The input contains multiple test cases.

For each test case, the first line contains one positive integers  n , the number of strings.  (1n100000)

Each of the next  n  lines contains a string  si  consisting of only lower case letters.  (1|si|100000,|si|106)
 

Output
For each test case, output " Case # x y " in one line (without quotes), where  x  indicates the case number starting from  1  and  y  denotes the answer of corresponding case.
 

Sample Input
       
       
1 a 2 aa bb 3 a ba abc
 

Sample Output
       
       
Case #1: 25 Case #2: 1323 Case #3: 18221
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值