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.
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)
1 #include <iostream> 2 #include <algorithm> 3 #include <stdio.h> 4 #include <cstring> 5 using namespace std; 6 const int mod = 1e9+7; 7 const int maxn=1e5+10; 8 struct Node{ 9 int ans[maxn+10]; 10 int id; 11 bool operator < (const Node &a) const 12 { 13 for(int i = maxn-1; i >= 0; i--) 14 { 15 if(ans[i] > a.ans[i]) return 1; 16 else if(ans[i] < a.ans[i]) return 0; 17 else ; 18 } 19 } 20 }node[100]; 21 long long x[maxn]; 22 void init(){ 23 x[0]=1; 24 for(int i=1;i<=maxn;i++){ 25 x[i]=x[i-1]*26; 26 x[i]%=mod; 27 } 28 x[maxn]=1; 29 } 30 bool Sort(Node a,Node b){ 31 for(int i=0;i<maxn;i++){ 32 if(a.ans[i]<b.ans[i]){ 33 return 1; 34 }else if(a.ans[i]>b.ans[i]){ 35 return 0; 36 } 37 } 38 } 39 int flag[maxn]; 40 int zero[maxn]; 41 int cnt=1; 42 int main(){ 43 init(); 44 int n; 45 while(~scanf("%d",&n)){ 46 memset(flag,-1,sizeof(flag)); 47 memset(node,0,sizeof(node)); 48 memset(zero,0,sizeof(zero)); 49 char a[maxn]; 50 for(int i=0;i<n;i++){ 51 scanf("%s",a); 52 int len=strlen(a); 53 if(len!=1){ 54 zero[a[0]-'a']=1; 55 } 56 for(int i=0;i<len;i++){ 57 node[a[i]-'a'].ans[len-i-1]++; 58 } 59 } 60 // cout<<"A"<<endl; 61 for(int i=0;i<26;i++){ 62 for(int j=0;j<maxn;j++){ 63 if(node[i].ans[j]>=26){ 64 node[i].ans[j+1]+=node[i].ans[j]/26; 65 node[i].ans[j]%=26; 66 } 67 } 68 node[i].id=i; 69 } 70 //cout<<"B"<<endl; 71 sort(node,node+26); 72 for(int i=0;i<26;i++){ 73 flag[node[i].id]=26-i-1; 74 } 75 76 for(int i=0;i<26;i++){ 77 if(zero[node[i].id]&&flag[node[i].id]==0){ 78 for(int j=25;j>=0;j--){ 79 if(zero[node[j].id]==0){ 80 for(int k=25;k>=j+1;k--){ 81 flag[node[k].id]=flag[node[k-1].id]; 82 } 83 flag[node[j].id]=0; 84 break; 85 } 86 } 87 break; 88 } 89 } 90 long long ans=0; 91 for(int i=0;i<26;i++){ 92 for(int j=0;j<maxn;j++){ 93 ans+=(x[j]*node[i].ans[j]*flag[node[i].id]%mod)%mod; 94 } 95 } 96 printf("Case #%d: %lld\n",cnt++,ans%mod); 97 } 98 return 0; 99 }