Balala Power!
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 3757 Accepted Submission(s): 907
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.
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)
a
//进位和去前导零!!!;统计每个字符在某一位置出现的次数(共1e5个位置),然后贪心一定是贡献值最大的
//用25,把统计出来的数组按照高位出现次数大的排序(可以按照数组排序),然后算就行了。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const ll mod=1e9+;
const int maxl=;
int pre[];
ll f[maxl];
struct Lu{
int id,ss[maxl];
ll v;
bool operator < (const Lu &p)const{
for(int i=maxl-;i>=;i--){
if(ss[i]>p.ss[i]) return ;
else if(ss[i]<p.ss[i]) return ;
}
}
}L[];
void init(){
f[]=;
for(int i=;i<maxl;i++){
f[i]=f[i-]*%mod;
}
}
int main()
{
init();
int cas=,n;
char str[maxl];
while(scanf("%d",&n)==){
memset(pre,,sizeof(pre));
memset(L,,sizeof(L));
for(int i=;i<n;i++){
scanf("%s",str);
int len=strlen(str)-;
if(len!=)
pre[str[]-'a']=;
for(int j=len;j>=;j--){
int id=str[j]-'a';
L[id].ss[len-j]++;
}
}
for(int i=;i<;i++){
L[i].id=i;
for(int j=;j<maxl-;j++){
if(L[i].ss[j]>=){
L[i].ss[j+]+=L[i].ss[j]/;
L[i].ss[j]%=;
}
}
}
sort(L,L+);
for(int i=;i<;i++)
L[i].v=-i;
if(pre[L[].id]){ //去前导零时可不是直接交换
for(int i=;i>=;i--){
if(!pre[L[i].id]){
for(int j=;j>i;j--)
L[j].v=L[j-].v;
L[i].v=;
break;
}
}
}
ll sum=;
for(int i=;i<;i++){
for(int j=maxl-;j>=;j--){
sum+=L[i].v*f[j]*L[i].ss[j]%mod;
sum%=mod;
}
}
printf("Case #%d: %lld\n",++cas,sum);
}
return ;
}