hdu6034

来源:互联网 发布:有什么看书软件 编辑:程序博客网 时间:2024/05/22 07:53

Balala Power!

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


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. (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 #xy" in one line (without quotes), where x indicates the case number starting from 1 and y denotes the answer of corresponding case.
 

Sample Input
1a2aabb3abaabc
 

Sample Output
Case #1: 25Case #2: 1323Case #3: 18221
思路:在多校联赛上一直没有做出来,后面清题时才发现如果直接存权值的话long long int根本不够用,如果把权值直接取模的话,那么就会影响后面的赋值,因此需要开一个二维数组来记录权值,a[i][j]代表第i个字母在j位置出现可了多少次,这是26进制的,所以满26向前面进一位,一开始用结构体做,结果TLE了,后面发现用多个数组进行排序其实更快。其实,一个数组可以通过另一个数组来排序。
代码:
#include <iostream>#include <stdio.h>#include <algorithm>#include <string.h>#include <cmath>const long long int MOD=1e9+7;using namespace std;long long int poww(int n,int m){    long long int a=1;    while(m)    {        if(m&1)a=(a*n)%MOD;        n=(n*n)%MOD;        m>>=1;    }}int a[27][100020];int flag[27];int v[27];int ss[27];bool cmp(int x,int y){    int i;     for(i=100019;i>=0;i--)     {         if(a[x][i]>a[y][i])return true;         if(a[x][i]<a[y][i])return false;     }     return false;}int main(){    int m;    int sss=1;    while(scanf("%d",&m)!=EOF)    {        int i;        memset(a,0,sizeof(a));        memset(flag,0,sizeof(flag));        memset(ss,0,sizeof(ss));        memset(v,-1,sizeof(v));        char c[100005];        for(i=0;i<m;i++)        {            scanf("%s",c);            int n=strlen(c);            if(n>1)flag[c[0]-'a'+1]=1;            for(int j=0;j<n;j++)            {               a[c[j]-'a'+1][n-j-1]++;            }        }        for(i=1;i<=26;i++)        {            for(int j=0;j<100020;j++)            {                if(a[i][j]>=26)                {                    a[i][j+1]+=a[i][j]/26;                    a[i][j]%=26;                }            }        }        int s[27];        for(i=1;i<=26;i++)        {            s[i]=i;        }        sort(s+1,s+1+26,cmp);        int k=25;        for(i=26;i>=1;i--)        {            if(flag[s[i]]!=1)            {                v[s[i]]=0;                break;            }        }        for(i=1;i<=26;i++)        {            int x=s[i];            if(v[x]==-1)            {                v[x]=k--;            }        }        long long int ans=0;        long long int e=1;        for(i=0;i<100020;i++)        {            for(int j=1;j<=26;j++)            {                ans=(ans+((e*a[j][i])%MOD*v[j])%MOD)%MOD;            }            e*=26;            e%=MOD;        }       printf("Case #%d: %lld\n",sss++,ans);    }    return 0;}