1002 Balala Power!

                Balala Power!

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


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 froma to z into each number ranged from 0 to25, 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 base26 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
题目大意:n个字符串,将a-z挑出对应的字母,赋值为0-25,使每个字符串对应与一个26进制的数,要求在不出现前导零的情况下,使这些26进制的数的和最大。

解题思路:先求每个字母在所有字符串所占的比重,然后根据每个字母所占的比重进行排序,将0到25个数字赋给a到z,然后考虑前导零的情况,若当前字母不能作为前导零,则依次往后类推,把可以作为前导零的往前换。
#include <bits/stdc++.h>
using namespace std;
const int N=100020;
const int mod=1e9+7;
int n;
int L;///记录字符串组中的最大长度
int num[26][N];
int power[26];///power[i]代表26的i次幂
int sum[N];
bool ban[26];

char str[N];///存储输入的每组字符串
int a[26];///

bool cmp(int A,int B)
{
    for(int i=L-1;i>=0;i--)///从最高位开始比较
    {
        if(num[A][i]!=num[B][i])
          return num[A][i]<num[B][i];
    }
    return 0;
}
void work()
{
    memset(num,0,sizeof(num));
    memset(ban,0,sizeof(ban));
    memset(sum,0,sizeof(sum));
    L=0;
    for(int i=0;i<n;i++)
    {
        scanf("%s",str);
        int len=strlen(str);
        if(len>1)
        {
            ban[str[0]-'a']=1;///记录第一个字母是否出现过
        }
        reverse(str,str+len);///字符串反转
        for(int j=0;j<len;j++)
        {
            num[str[j]-'a'][j]++;///在第j位上每个字母出现的次数,想一下表
            sum[str[j]-'a']+=power[j];///每个字母出现在j位上,加上26的j次幂
            if(sum[str[j]-'a']>=mod)
            {
                sum[str[j]-'a']-=mod;
            }
        }
        L=max(L,len);
    }

    for(int i=0;i<26;i++)///将每个字母的数量转化成26进制数
    {
        for(int j=0;j<L;j++)
        {
            num[i][j+1]+=num[i][j]/26;///满26进一,将每个字母都转换成26进制数
            num[i][j]%=26;
        }
        while(num[i][L])
        {
            num[i][L+1]+=num[i][L]/26;
            num[i][L++]%=26;
        }
        a[i]=i;///每个字母的值初始化
    }

    sort(a,a+26,cmp);
    for(int i=0;i<26;i++)
        cout<<a[i]<<" ";
    cout<<endl;
    int zero=-1;
    for(int i=0;i<26;i++)///找到可以为零的字母
    {
        if(!ban[a[i]])
        {
            zero=a[i];
            cout<<zero<<endl;
            break;
        }
    }
    int res=0,x=25;
    for(int i=25;i>=0;i--)///从后往前对字母依次进行赋值
    {
        if(a[i]!=zero)
        {
            res+=(long long)(x--)*sum[a[i]]%mod;
            res%=mod;
        }
    }
    static int t=0;
    printf("Case #%d: %d\n",++t,res);
}
int main()
{
    power[0]=1;///power[i]代表26的i次幂
    for(int i=1;i<N;i++)
    {
        power[i]=(long long)power[i-1]*26%mod;
    }
    while(scanf("%d",&n)!=EOF)
    {
        work();
    }
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值