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)
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
启发博客:http://blog.csdn.net/qq_38576126/article/details/76154963
其实是一道思路很顺畅的题,将每一个字母在每个位置上的出现次数记录一下,做个排序,注意前导零的情况。
奇怪的是比赛的时候也想到了这样的情况,但是一直WA,看来是写法的问题,还是写的题不够多。
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<queue> 5 #include<algorithm> 6 #include<cmath> 7 using namespace std; 8 9 const int MOD=1e9+7; 10 const int maxn=1e5+5; 11 struct word 12 { 13 int pos;//表示第几个字母 14 int time[maxn];//各个字母在各位置出现的次数 15 }q[27]; 16 //不加&的话会TLE,用&比不用快 17 bool cmp(word &a,word &b)//从小到大排序 18 { 19 for(int i=maxn-1;i>1;i--) 20 { 21 if(a.time[i]!=b.time[i]) 22 return a.time[i]<b.time[i]; 23 } 24 return a.time[1]<b.time[1]; 25 } 26 27 int main() 28 { 29 int n,len; 30 char s[maxn]; 31 int t=1; 32 while(~scanf("%d",&n)) 33 { 34 int flag[27]={0}; 35 //用来标记各个字母是否可以对应0,从0开始 36 //0表示可以,1表示不可以 37 for(int i=0;i<26;i++) 38 { 39 q[i].pos=i; 40 for(int j=0;j<maxn;j++) 41 q[i].time[j]=0; 42 } 43 while(n--) 44 { 45 scanf("%s",&s); 46 len=strlen(s); 47 if(len>1)flag[s[0]-'a']=1; 48 //不能对应0 49 for(int i=0;i<len;i++) 50 { 51 int y=len-i;//位置反着记 52 q[s[i]-'a'].time[y]++; 53 //开始做进位处理 54 while(q[s[i]-'a'].time[y]==26) 55 { 56 q[s[i]-'a'].time[y]=0; 57 y++; 58 q[s[i]-'a'].time[y]++; 59 } 60 } 61 } 62 sort(q,q+26,cmp);//把26个字母按从小到大的赋值排好序 63 //开始处理前导0 64 int op;//op即赋前导0的位置 65 for(int i=0;i<26;i++) 66 { 67 if(flag[q[i].pos]==0) 68 { 69 op=i; 70 break; 71 } 72 } 73 long long sum=0,sumi=0,num,m=1; 74 for(int i=0;i<26;i++) 75 { 76 sumi=0; 77 if(i==op) 78 num=0; 79 else 80 { 81 num=m; 82 m++; 83 } 84 for(int j=maxn-1;j>0;j--) 85 { 86 sumi=(sumi*26)%MOD; 87 sumi=(sumi+(long long)q[i].time[j]*num)%MOD; 88 } 89 sum=(sum+sumi)%MOD; 90 } 91 printf("Case #%d: %lld ",t++,sum); 92 } 93 return 0; 94 }