Balala Power!

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 a to 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.


输入

The input contains multiple test cases.

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

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

输出

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.

样例输入

1
a
2
aa
bb
3
a
ba
abc

样例输出

Case #1: 25
Case #2: 1323
Case #3: 18221
题目大意就是给出n个26进制数,但是用字母表示每一位,要求n数之和最大输出这个和

做法:如果是十进制的数,我们都会想,将每个字母的个数记录下来,然后进行求值,如 ab 则a有10个,b有一个,则a选9,b选8  ab最大的情况就是  98 那么换成26进制同样如此,只不过要注意用数组来记录个数,因为给出的数字太大,long long int 也无法保存字母的个数


#include<iostream>//这个代码有误,思路没问题但提示越界
#include<string>
#include<cstring>
#include<stdio.h>
#include <algorithm>
#include<math.h>
using namespace std;
struct qz
{
    int t;
    int ms[700]={0};//存26进制的个数
    long long int st=0;//直接得到10进制的个数(对1000000007取余)
};
long long int mod(qz a,qz b)//进行出现次数的排列
{
    return a.st<b.st;
}
int main()
{
    int k=1;
    long long int f[180];
    f[0]=1;
    for(int i=1;i<120;i++)//计算26进制转10进制的每一位
    {
        f[i]=f[i-1]*26%1000000007;
    }
    int n;
    while(cin>>n)
    {
        int b[40]={0};
        int s=0;
        qz a[40];
        for(int i=0;i<26;i++)
            a[i].t=i;
        while(n--)
        {
            string s;
            cin>>s;
            int j=0;
            if(s.length()>0)//首位不为0判断
            b[s[0]-'a']=1;
            for(int i=s.length()-1;i>=0;i--)//计算每个字母的数目
            {
                a[s[i]-'a'].ms[j]++;
                int h=j;
                j++;
                while(a[s[i]-'a'].ms[h]==26)
                {

                    a[s[i]-'a'].ms[h]=0;
                    h++;
                    a[s[i]-'a'].ms[h]++;
                }
            }
        }
        for(int i=0;i<26;i++)
        {
            int j=120;
            while(a[i].ms[j]==0)
            {
                j--;
                if(j<0)
                {
                    s++;
                    break;
                }

            }
            for(int m=0;m<=j;m++)//直接转换成10进制
            {

                a[i].st+=a[i].ms[m]*f[m];
                a[i].st=a[i].st%1000000007;
            }
        }
        sort(a,a+26,mod);
        long long int u=0;
        if(s==0)//首位不为0判断
        {
            int y=1;
            for(int i=0;i<=25;i++)
            {
                if(s==0&&b[a[i].t]==0)
                {
                    u+=0;
                    s=1;
                }
                else
                {
                    u+=y*a[i].st;
                    u=u%1000000007;
                    y++;
                }
            }
        }
        else
        {
            for(int i=s;i<=25;i++)
            {
                u+=i*a[i].st;
                u=u%1000000007;
            }
        }
        printf("Case #%d: %lld\n",k++,u);
    }
    return 0;
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值