Balala Power!
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 1479 Accepted Submission(s): 244
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
Source
#include<string>
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;
#define MAXN 100020
const int mod=1e9+7;
char str[MAXN];
struct node
{
int a[MAXN];
int letter;
}num[27];
int visit[27];
int sum[27];
int len,max_len;
int cmp(node x,node y)
{
for(int i=max_len-1;i>=0;i--)
if(x.a[i]!=y.a[i])
return x.a[i]<y.a[i];
return 0;
}
int powr[MAXN];
void init()
{
powr[0]=1;
for(int i=1;i<MAXN;i++)
powr[i]=(long long)powr[i-1]*26%mod;
}
int main()
{
int n;
init();
int cases=1;
//freopen("in.txt","r",stdin);
while(scanf("%d",&n)!=EOF)
{
len=max_len=0;
memset(num,0,sizeof(num));
memset(sum,0,sizeof(sum));
memset(visit,0,sizeof(visit));
for(int i=0;i<n;i++){
scanf("%s",str);
len=strlen(str);
max_len=max(len,max_len);
if(len>1)
visit[str[0]-'a']=1;
reverse(str,str+len);
for(int j=0;j<len;j++){
num[str[j]-'a'].a[j]++;///如果在这里处理进位会耗费大量时间
sum[str[j]-'a']+=powr[j];
if(sum[str[j]-'a']>=mod)
sum[str[j]-'a']-=mod;
}
}
///进位
for(int i=0;i<26;i++){
for(int j=0;j<max_len;j++){
num[i].a[j+1]+=num[i].a[j]/26;
num[i].a[j]%=26;
}
while(num[i].a[max_len])
{
num[i].a[max_len+1]=num[i].a[max_len]/26;
num[i].a[max_len++]%=26;
}
}
int flag=-1;
for(int i=0;i<26;i++)
num[i].letter=i;
sort(num,num+26,cmp);
for(int i=0;i<26;i++){
if(visit[num[i].letter]==0){
flag=num[i].letter;
break;
}
}
int ans=0,cnt=25;
for(int i=25;i>=0;i--){
if(flag!=num[i].letter)
ans=(ans+(long long)(cnt--)*sum[num[i].letter]%mod)%mod;
}
printf("Case #%d: %d\n",cases++,ans);
}
return 0;
}