Balala Power!
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 3598 Accepted Submission(s): 856
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
//贪心,记录每个字母在每个位置出现的次数, (注意:满26要进位) //记录开头字母,先判定应该给哪个字母赋值0 //最后给越早越多出现的字母给最大,依次赋值,最后计算总和 //菜鸡我想不到其他优化,1800ms卡过......... #include <stdio.h> #include <map> #include <iostream> #include <string.h> #include <algorithm> #define maxn 100000+50 #define inf 0x3f3f3f3f #define ll long long using namespace std; ll mod = 1e9+7; char s[maxn]; ll mp[maxn][26]; ll vis[maxn]; bool jilu[26]; ll mm[maxn]; struct Node { int x; int num[maxn]; }node[26]; int cmp(const Node &a, const Node &b) { for(int i=0; i<maxn; i++) { if(a.num[i] != b.num[i]) return a.num[i] > b.num[i]; } return a.num[maxn-1] > b.num[maxn-1]; } int main() { int n, kk=0; while(~scanf("%d", &n)) { kk++; int zero = 0; ll h = 25, sum = 0; memset(mp, 0, sizeof(mp)); memset(vis, 0, sizeof(vis)); memset(jilu, 0, sizeof(jilu)); memset(mm, 0, sizeof(mm)); for(int i=0; i<26; i++) { node[i].x = i; memset(node[i].num, 0, sizeof(node[i].num)); } for(int i=0; i<n; i++) { scanf("%s", s); int l = strlen(s); int k = maxn-l; for(int j=k; j<maxn; j++) { mp[j][s[j-k]-'a']++; } if(l!=1) jilu[s[0]-'a'] = 1; } for(int i=0; i<26; i++) { for(int j=maxn-1; j>0; j--) { if(mp[j][i]>=26) { mp[j-1][i] += mp[j][i]/26; mp[j][i] = mp[j][i]%26; } } } for(int i=0; i<26; i++) { for(int j=maxn-1; j>=0; j--) { node[i].num[j] += mp[j][i]; } } sort(node, node+26, cmp); for(int i=25; i>=0; i--) { if(!jilu[node[i].x]) { zero = node[i].x; break; } } for(int i=0; i<26; i++) { if(node[i].x != zero) vis[node[i].x] = h--; } for(int i=maxn-1; i>=0; i--) { for(int j=0; j<26; j++) mm[i] += mp[i][j]*vis[j]; } ll jk = 1; sum = mm[maxn-1]; for(int i=maxn-2; i>=0; i--) { jk = (jk * 26)%mod; sum = (sum + mm[i] * jk)%mod; } printf("Case #%d: %lld\n", kk, sum); } return 0; }