Balala Power!
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 2709 Accepted Submission(s): 577
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
Recommend
题意:给一堆字符串,每个字符串相当于一个26进制数,每个字母替代0-25中的一个数字,问所有26进制数的和加起来最大是多少.
对于每个字母都记录一下在第几位出现过多少次,相当于把每个字母表示成一个二十六进制数,最后根据每个字母的二十六进制数大小排个序.
对于不能前导0的处理,记录一下哪些字母不能作为0.然后从排序好的结果里,选出一个最小的可以为0的字母作为0即可.
#include <cstring>
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <cmath>
using namespace std;
const int MAXN=410010;
const long long MOD=1e9+7;
const double INF=1e9+7;
const double EPS=1e-8;
long long num[26][MAXN];
int MAX[26];
char s[MAXN];
double res[26];
int vis[26];
int ans[26];
int cmp(int x,int y){
return res[x]>res[y];
}
long long C[MAXN];
double tt[26];
int main(){
int n;
int cas=1;
C[0]=1;
for(int i=1;i<100001;i++){
C[i]=(C[i-1]*26)%MOD;
}
//freopen("1002.in","r",stdin);
while(scanf("%d",&n)!=EOF){
memset(num,0,sizeof(num));
memset(MAX,-1,sizeof(MAX));
memset(vis,0,sizeof(vis));
for(int i=0;i<26;i++){
ans[i]=i;
res[i]=-INF;
}
for(int i=0;i<n;i++){
scanf("%s",s);
int len=strlen(s);
for(int j=0;j<len;j++){
int x=s[j]-'a';
num[x][len-j-1]++;
MAX[x]=max(MAX[x],len-j-1);
}
//不能作为0的数
if(len>1){
vis[s[0]-'a']=1;
}
}
int cnt=0;//统计不同的字母个数
for(int i=0;i<26;i++){
if(MAX[i]!=-1){
cnt++;
}
}
int flag=-1;
if(cnt==26){
flag=1;//有26个字母
}
int tot=26;
for(int i=0;i<26;i++){
for(int j=0;j<MAX[i];j++){
while(num[i][j]>=26){
int x=num[i][j]/26;
num[i][j]-=x*26;
num[i][j+1]+=x;
}
}
while(num[i][MAX[i]]>=26){
int x=num[i][MAX[i]]/26;
num[i][MAX[i]]-=x*26;
MAX[i]++;
num[i][MAX[i]]+=x;
}
}
while(tot){
for(int i=0;i<tot-1;i++){
int xx=ans[i];
int yy=ans[i+1];
if(MAX[xx]<MAX[yy]){
swap(ans[i],ans[i+1]);
}else if(MAX[xx]==MAX[yy]){
for(int k=MAX[xx];k>=0;k--){
if(num[xx][k]<num[yy][k]){
swap(ans[i],ans[i+1]);
break;
}
else if(num[xx][k]>num[yy][k])
break;
}
}
}
tot--;
}
long long xx=25;
long long sum=0;
if(flag!=-1){
for(int i=25;i>=0;i--){
if(!vis[ans[i]]){
flag=ans[i];
break;
}
}
}
for(int k=0;k<cnt;k++){
int i=ans[k];
if(i==flag){
continue;
}
long long temp=0;
for(int j=0;j<=MAX[i];j++){
temp=(temp+(num[i][j]*C[j])%MOD)%MOD;
}
temp=(temp*xx)%MOD;
xx--;
sum=(sum+temp)%MOD;
}
printf("Case #%d: %lld\n",cas++,sum);
}
}