Balala Power!
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 4247 Accepted Submission(s): 1055
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 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
题目大意:求用多个a,b,c,...,z表示的26进制数字的总和,其中a,b,c,...,z与0~25的对应关系不确定,要求不能有前缀0,求出最大的总和。
解题思路:先算出每一位上a,b,c,...,z的个数,然后分别组成26个26进制数,通过基数排序从低到高比较每个字母的数量(包括进位),排出a,b,c,...,z的权值大小,为避免前缀0,在讲权值最小的非首字母的字母权值定为0,其他顺序不变,然后根据每一位字母的个数和字母的权值,求出最后的总和。
代码如下:
#include <bits/stdc++.h>
using namespace std;
#define inf 1000000007
#define LL long long
int a[110000][26];
struct aaa
{
LL sum;
bool flag;
int id;
}b[26];
bool operator <(aaa a,aaa b)
{
return a.sum<b.sum;
}
LL qmod(LL a,LL b,LL n) //快速幂去摸
{
LL ret=1;
LL tmp=a;
while(b)
{
if(b&0x1) ret=ret*tmp%n;
tmp=tmp*tmp%n;
b>>=1;
}
return ret;
}
void sortt()//稳定的排序
{
int i,j;
for(i=1;i<26;i++)
{
for(j=i;j>0;j--)
{
if(b[j]<b[j-1])
swap(b[j],b[j-1]);
else
break;
}
}
}
int main()
{
std::ios::sync_with_stdio(false);
string s;
int n,t=0,i,j,l,ma,tmp[26];//tmp保存进位
LL ans;
while(cin>>n)
{
t++;
memset(a,0,sizeof(a));
for(i=0;i<26;i++)
{
b[i].flag=0;
b[i].sum=0;
b[i].id=i;
}
ma=0;
for(i=0;i<n;i++)
{
cin>>s;
l=s.length();
b[s[0]-'a'].flag=1;
ma=max(ma,l);
for(j=0;j<l;j++)
{
a[l-1-j][s[j]-'a']++;
}
}
memset(tmp,0,sizeof(tmp));
for(i=0;i<=ma+100;i++)
{
for(j=0;j<26;j++)
{
b[j].sum=(tmp[b[j].id]+a[i][b[j].id])%26;
tmp[b[j].id]=(tmp[b[j].id]+a[i][b[j].id])/26;
}
sortt();
}
//避免首0
for(i=0;i<26;i++)
{
if(b[i].flag==0)
{
break;
}
}
for(;i>0;i--)
{
swap(b[i],b[i-1]);
}
ans=0;
for(i=25;i>=0;i--)
{
for(j=0;j<ma;j++)
{
ans+=qmod(26,j,inf)*a[j][b[i].id]*i;
ans=ans%inf;
}
}
cout<<"Case #"<<t<<": "<<ans<<endl;
}
return 0;
}