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 26hilariously.
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.
InputThe 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) OutputFor 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 Input1a2aabb3abaabc Sample OutputCase #1: 25Case #2: 1323Case #3: 18221题意:将字符替换成一个数字,每个字符串各组成一个数(26进制),然后加起来和最大解法:1 很自然想到是25 24 23这样的替换顺序2 考虑高精度带来的误差(包括进位和移位)3 0放哪里,怎么处理简直坑到吐4 倒序保存便于处理
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <cstring>
using namespace std;
const int mod = 1e9+;
const int maxn=1e5+;
struct Node{
int ans[maxn+];
int id;
bool operator < (const Node &a) const
{
for(int i = maxn-; i >= ; i--)
{
if(ans[i] > a.ans[i]) return ;
else if(ans[i] < a.ans[i]) return ;
else ;
}
}
}node[];
long long x[maxn];
void init(){
x[]=;
for(int i=;i<=maxn;i++){
x[i]=x[i-]*;
x[i]%=mod;
}
x[maxn]=;
}
bool Sort(Node a,Node b){
for(int i=;i<maxn;i++){
if(a.ans[i]<b.ans[i]){
return ;
}else if(a.ans[i]>b.ans[i]){
return ;
}
}
}
int flag[maxn];
int zero[maxn];
int cnt=;
int main(){
init();
int n;
while(~scanf("%d",&n)){
memset(flag,-,sizeof(flag));
memset(node,,sizeof(node));
memset(zero,,sizeof(zero));
char a[maxn];
for(int i=;i<n;i++){
scanf("%s",a);
int len=strlen(a);
if(len!=){
zero[a[]-'a']=;
}
for(int i=;i<len;i++){
node[a[i]-'a'].ans[len-i-]++;
}
}
// cout<<"A"<<endl;
for(int i=;i<;i++){
for(int j=;j<maxn;j++){
if(node[i].ans[j]>=){
node[i].ans[j+]+=node[i].ans[j]/;
node[i].ans[j]%=;
}
}
node[i].id=i;
}
//cout<<"B"<<endl;
sort(node,node+);
for(int i=;i<;i++){
flag[node[i].id]=-i-;
} for(int i=;i<;i++){
if(zero[node[i].id]&&flag[node[i].id]==){
for(int j=;j>=;j--){
if(zero[node[j].id]==){
for(int k=;k>=j+;k--){
flag[node[k].id]=flag[node[k-].id];
}
flag[node[j].id]=;
break;
}
}
break;
}
}
long long ans=;
for(int i=;i<;i++){
for(int j=;j<maxn;j++){
ans+=(x[j]*node[i].ans[j]*flag[node[i].id]%mod)%mod;
}
}
printf("Case #%d: %lld\n",cnt++,ans%mod);
}
return ;
}