1002.Balala Power!

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

Input
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)

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

题解如下:

每个字符对答案的贡献都可以看作一个 26 进制的数字,问题相当于要给这些贡献加一个 0 到 25 的权重使得答案最大。最大的数匹配 25,次大的数匹配 24,依次类推。排序后这样依次贪心即可,唯一注意的是不能出现前导 0。

//374MS 25884KB
#include<cstdio>  
#include<cstring>  
#include<algorithm>  
#define mod 1000000007  
#define maxn 100010  
typedef long long LL;  
using namespace std;  
bool d[1010];  //将出现在第一位的字母记录下来
char a[maxn];
LL un[maxn],num[30][maxn],sum[30];
//un来存第i位的十进制大小    num来存字母在某一位出现过的次数  sum记录串的大小
LL n,flag,maxl,res,x;  

bool cmp(int a,int b)  
{  
    for(int i=maxl-1;i>=0;i--)  
    {  
        if(num[a][i]!=num[b][i])  
            return num[a][i]<num[b][i];  
    }  
    return 0;  
}  

int main()  
{  
    un[0]=1;  
    for(int i=1;i<=maxn-2;i++)  
        un[i]=26*un[i-1]%mod;  
    while(scanf("%lld",&n)!=EOF)  
    {  
        maxl=0,flag=-1,res=0,x=25;  
        memset(sum,0,sizeof(sum));  
        memset(num,0,sizeof(num));  
        memset(d,false,sizeof(d));  
        for(int i=0;i<n;i++)  
        {  
            scanf("%s",a);  
            LL len=strlen(a);  
            if(len>1)    
            {  
                d[a[0]-'a']=true;  
            }  
            for(int j=0;j<len;j++)  
            {  
                num[a[j]-'a'][len-1-j]++;  
                sum[a[j]-'a']+=un[len-j-1];  
                sum[a[j]-'a']%=mod;  
            }  
            maxl=max(maxl,len);  
        }  
        for(int i=0;i<26;i++)  
        {  
            for(int j=0;j<maxl;j++)  
            {  
                num[i][j+1]+=num[i][j]/26;  
                num[i][j]%=26;  
            }  
            while(num[i][maxl])   
            {  
                num[i][maxl+1]+=num[i][maxl]/26;  
                num[i][maxl++]%=26;  
            }  
            a[i]=i;  
        }  
        sort(a,a+26,cmp);       //将每个字母的权重排序
        for(int i=0;i<26;i++)  
        {  
            if(!d[a[i]])  
            {  
                flag=a[i];  
                break;  
            }  
        }  
        for(int i=25;i>=0;i--)  
        {  
            LL cnt=flag;
            if(a[i]!=cnt)  
            {  
                res+=(x--)*sum[a[i]]%mod;  
                res%=mod;  
            }  
        }  
        static int p=1;   
        printf("Case #%d: %lld\n",p++,res);  
    }  
    return 0;  
}  
上一篇:D-POJ-3126 Prime Path


下一篇:《HTML5 canvas开发详解(第2版)》——2.8 用颜色和渐变填充对象