Balala Power!
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 4724 Accepted Submission(s): 1185
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
/*
n个字符串,将字幕用数字表示,然后以26进制计算这n个字符串数值累加和
例如
a
ba
abc
让a=25 b = 24 c = 23
a = 25 * 26^0
ba = 24*26^1+25*26^0
abc = 25*26^2+24*26^1+23*26^0
sum = 25*26^0+ 24*26^1+25*26^0+25*26^2+24*26^1+23*26^0
= a(26^0+26^0+26^2) + b(26^1+26^1)+c(26^0)
所以()越大的分配数值越大越有利,暂且把()叫做系数
而系数是26的次方非常大,没法比较,这里采取高精思想手敲一个26进制,
例如a(26^0+26^0+26^2)对应的是102
*/
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string.h>
#include <stdlib.h>
#include <set>
#include <algorithm>
using namespace std;
typedef unsigned long long ull;
const int maxn = 100000+10;
const ull mod = 1e9+7;
ull weight[maxn];///26进制的权值
int f[27][maxn]; ///每个字母的系数,用26进制表示
ull w[26];///每个字母的系数,十进制
int a[26];///给26个字母按照系数排序
bool iffirst[26];///该字母是否首字母
char s[maxn];
int n;
void init(){
weight[0] = 1;
for(int i = 1;i<maxn;++i){
weight[i] = (weight[i-1]*26)%mod;
}
for(int i = 0;i<26;++i){
a[i] = i;///字母默认排序a~z
}
}
bool cmp(int a,int b){
if(f[a][0] != f[b][0])
return f[a][0] > f[b][0];
for(int i = f[a][0];i>0;--i){
if(f[a][i]!=f[b][i])
return f[a][i]>f[b][i];
}
return 0;///全相等谁排在前面无所谓
}
int main()
{
init();
int t = 0;
while(scanf("%d",&n)!=EOF){
memset(f,0,sizeof(f));
memset(iffirst,false,sizeof(iffirst));
memset(w,0,sizeof(w));
for(int i = 0;i<n;++i){
scanf("%s",s);
iffirst[s[0]-'a'] = true;
int len = strlen(s);
reverse(s,s+len);
for(int j = 0; j<len;++j){
int k = s[j]-'a';
f[k][j+1]++;
if(j+1>f[k][0]) f[k][0] = j+1;///f[i][0]存长度
w[k] = (w[k]+weight[j])%mod;
}
}
///进位操作
for(int i = 0;i<26;++i){
int len = f[i][0];
///将所有进位都堆积在f[i][len+1]这一位上了。
for(int j = 1;j<=len;++j){
f[i][j+1] += f[i][j]/26;
f[i][j] %= 26;
}
///对f[i][len+1]进行进位
len += 1;
while(f[i][len]>0){
f[i][len+1] += f[i][len]/26;
f[i][len] %= 26;
len++;
}
f[i][0] = len;
}
sort(a,a+26,cmp);///以系数为依据对字母从大到小排序。
int zero = 0;///标记权值为0的字母
for(int i = 25;i>=0;i--){
if(!iffirst[a[i]]){
zero = a[i];
break;
}
}
ull ans = 0;
int x = 25;
for(int i = 0;i<26;++i){
if(zero ==a[i]) continue;
ans += (w[a[i]] *x) %mod;
x--;
ans%=mod;
}
printf("Case #%d: %lld\n",++t,ans);
}
return 0;
}