|
Balala Power!Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 1531 Accepted Submission(s): 256
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 zinto 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)
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
Sample Output
Source
|
题意:给你n个由小写字母组成的字符串,让你给26个字母分配0-25,每个字符串形成一个26进制的数字,使这n个数的和尽可能大。(不能有前导0,但是单个0可以);
思路:利用26进制的思路去写,统计每一个字符处在每个位置的个数(满26注意进位),判断大小决定谁为0时,不能用sum和,因为取余了(我就那么天真的这么写了),按照其所处的位置大小进行排位,优先选出前导0的字符(贪心的味道),其余的一般处理,学习一下这个po数组的姿势;
//#pragma GCC optimize ("O2")
//#include<bits/stdc++.h>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define mod 1000000007
#define max_n 100010
typedef long long LL;
using namespace std;
bool d[1010];
char a[max_n];
LL po[max_n],num[30][max_n],sum[30];
LL n,ans,cnt,res,x;
bool cmp(int A,int B)
{
for(int i=ans-1;i>=0;i--)
{
if(num[A][i]!=num[B][i])
return num[A][i]<num[B][i];
}
return 0;
}
int main()
{
po[0]=1;
for(int i=1;i<=max_n-2;i++)
po[i]=26*po[i-1]%mod;
while(scanf("%lld",&n)==1)
{
ans=0,cnt=-1,res=0,x=25;
memset(sum,0,sizeof(sum));
memset(num,0,sizeof(num));
memset(d,false,sizeof(d));
for(int i=0;i<n;i++)
{
scanf("%s",a);
LL len=strlen(a);
if(len>1) //bug点
{
d[a[0]-'a']=true;
}
for(int j=0;j<len;j++)
{
num[a[j]-'a'][len-1-j]++;
sum[a[j]-'a']+=po[len-j-1];
sum[a[j]-'a']%=mod;
}
ans=max(ans,len);
}
for(int i=0;i<26;i++)
{
for(int j=0;j<ans;j++)
{
num[i][j+1]+=num[i][j]/26;
num[i][j]%=26;
}
while(num[i][ans]) //bug点
{
num[i][ans+1]+=num[i][ans]/26;
num[i][ans++]%=26;
}
a[i]=i;
}
sort(a,a+26,cmp);
for(int i=0;i<26;i++)
{
if(!d[a[i]])
{
cnt=a[i];
break;
}
}
for(int i=25;i>=0;i--)
{
if(a[i]!=cnt)
{
res+=(x--)*sum[a[i]]%mod;
res%=mod;
}
}
static int p=1; //新姿势
printf("Case #%d: %lld\n",p++,res);
}
return 0;
}