Balala Power!
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 3594 Accepted Submission(s): 855
Problem Description
![](https://i-blog.csdnimg.cn/blog_migrate/206c742d60bb80a1bf080aa3f1c098a0.jpeg)
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 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
#include<cstdio> #include<cstdlib> #include<cstring> #include<string> #include<algorithm> #include<iostream> #include<queue> #include<map> #include<cmath> #include<set> #include<stack> #define ll long long #define pb push_back #define max(x,y) ((x)>(y)?(x):(y)) #define min(x,y) ((x)>(y)?(y):(x)) #define cls(name,x) memset(name,x,sizeof(name)) #define pos first #define index second #define mp make_pair using namespace std; const int inf=1e9+10; const ll llinf=1e16+10; const int maxn=1e5+10; const int maxm=2e5+10; const int mod=1e9+7; int n; int p[maxn][30]; char s[maxn]; int lead[30]; int key[30]; int maxk; bool cmp(int x,int y)//如果x的优先级比y要高,则将x后移,y前移 { for(int i=maxk;i>=0;i--) { if(p[i][x]>p[i][y]) return true; else if(p[i][x]<p[i][y]) return false; } return false; } int main() { //freopen("in.txt","r",stdin); int cas=1; while(~scanf("%d",&n)) { maxk=0; cls(lead,0); cls(p,0); for(int i=0;i<n;i++) { scanf("%s",s); int k=0; int lens=strlen(s); for(int j=lens-1;j>=0;j--) { p[k++][s[j]-'a']++; if(j==0&&lens!=1) lead[s[j]-'a']=1; } maxk=max(maxk,k-1); } for(int i=0;i<=maxk;i++) { for(int j=0;j<26;j++) { if(p[i][j]>=26) { p[i+1][j]+=p[i][j]/26; p[i][j]%=26; maxk=max(maxk,i+1); } } } for(int i=0;i<26;i++) key[i]=i; for(int i=0;i<26;i++) for(int j=i+1;j<26;j++) { if(cmp(key[i],key[j])) swap(key[i],key[j]); } if(lead[key[0]]==1) { int pos,t; for(int i=1;i<26;i++) if(lead[key[i]]==0) { t=key[i]; pos=i;//将原本key[0]->0变为key[pos]->0 break; } for(int i=pos-1;i>=0;i--) key[i+1]=key[i]; key[0]=t; } int change[26]; for(int i=0;i<26;i++)//反向映射 { change[key[i]]=i; } ll k=1; ll ans=0; for(int i=0;i<=maxk;i++) { ll sum=0; for(int j=0;j<26;j++) { sum+=(ll)change[j]*p[i][j]; sum%=mod; } ans+=sum*k; ans%=mod; k=(k*26)%mod; } printf("Case #%d: %lld\n",cas++,ans); } return 0; }