Balala Power!
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 4809 Accepted Submission(s): 387
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.
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)
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define ll long long
const int N=105005;
const int mod=1000000007;
ll p[N];
int f[26],q[26],cnt[26][N];
char s[N];
int mysort(){
int i,j,k;
for(i=0;i<26;i++){
for(j=i+1;j<26;j++){
for(k=N-1;k>=0;k--){
if(cnt[q[i]][k]<cnt[q[j]][k]){
int t=q[i];
q[i]=q[j];
q[j]=t;
break;
}
if(cnt[q[i]][k]>cnt[q[j]][k])
break;
}
}
}
if(f[q[25]]==1)
{
for(i=24;i>=0;i--){
if(!f[q[i]]){
for(j=i;j<25;j++)
q[j]=q[j+1];
break;
}
}
}
}
void init(){
p[0]=1;
for(int i=1;i<N;i++)
p[i]=p[i-1]*26%mod;
}
int main(){
ll ans;
int i,j,n,m,x,t=1;
init();
while(~scanf("%d",&n)){
memset(cnt,0,sizeof(cnt));
memset(f,0,sizeof(f));
for(i=0;i<26;i++)
q[i]=i;
for(i=0;i<n;i++){
scanf("%s",s);
m=strlen(s);
for(j=0;j<m;j++){
cnt[s[j]-'a'][m-j-1]++;
}
f[s[0]-'a']=1;
}
for(i=0;i<26;i++){
for(j=0;j<N-1;j++){
cnt[i][j+1]+=cnt[i][j]/26;
cnt[i][j]%=26;
}
}
mysort();
for(ans=i=0;i<25;i++){
for(j=N-1;j>=0;j--)
{
if(cnt[q[i]][j]){
ans+=p[j]*cnt[q[i]][j]*(25-i),ans%=mod;
}
}
}
printf("Case #%d: %lld\n",t++,ans);
}
return 0;
}