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: 1323Case #3: 18221
比赛的时候脑子发热,是让求最大值然后取余,但是当时做成取余后的最大值(完全不一样)
思路还是比较简单,给你一些全是小写字母组成的串,然后让你译成0~26的数,问你能得到的最大和是多少,编译成数字后每个字符串就变成了一个26进制的数,那么要求最大和,必须要让字符所占权值最大的字母变成大的数,题意就转变成求每个字母出现位置的权值,仔细想想每个字母所对应的权值之和其实就是一个还未进位的26进制数(在某些位置上出现的次数大于26,需要后续操作进位后才是标准的26进制数),
接下来就是比较26进制的大小,类似于二进制,每一位的位权是他前边所有位权之和+1,所以从最后一位往前找,找到不一样的,大的就大,小的就小,还要注意离散化处理,因为26进制数排序后和原来的字母不在对应,所以需要离散化处理,题解中处理的非常巧妙,在排序的过程中,数不变,改变存其下标的数组(毕竟该数与下表是一一对应的),还要注意当过派头的字母不能变为0,这个就很好处理了标记一下就可以了,
附上标程代码:
ac代码:
#include <bits/stdc++.h> using namespace std; typedef long long LL; const int N = 100020; const int Q = 1e9 + 7; int n , L; int num[26][N]; int power[N] , sum[N]; bool ban[26]; char str[N]; int a[26]; bool cmp(int A , int B) { for (int i = L - 1 ; i >= 0 ; -- i) { if (num[A][i] != num[B][i]) { return num[A][i] < num[B][i]; } } return 0; } void work() { memset(num , 0 , sizeof(num)); memset(ban , 0 , sizeof(ban)); memset(sum , 0 , sizeof(sum)); L = 0; for (int i = 0 ; i < n ; ++ i) { scanf("%s" , str); int len = strlen(str); if (len > 1) { ban[str[0] - 'a'] = 1;//判断是否当过排头 } reverse(str , str + len); for (int j = 0 ; j < len ; ++ j) { ++ num[str[j] - 'a'][j];// sum[str[j] - 'a'] += power[j]; if (sum[str[j] - 'a'] >= Q) { sum[str[j] - 'a'] -= Q; } } L = max(L , len); } for (int i = 0 ; i < 26 ; ++ i) { for (int j = 0 ; j < L ; ++ j) { num[i][j + 1] += num[i][j] / 26; num[i][j] %= 26; } while (num[i][L]) { num[i][L + 1] += num[i][L] / 26; num[i][L ++] %= 26; } a[i] = i; } sort(a , a + 26 , cmp); int zero = -1; for (int i = 0 ; i < 26 ; ++ i) { if (!ban[a[i]]) { zero = a[i]; break; } } int res = 0 , x = 25; for (int i = 25 ; i >= 0 ; -- i) { if (a[i] != zero) { res += (LL)(x --) * sum[a[i]] % Q; res %= Q; } } static int ca = 0; printf("Case #%d: %d\n" , ++ ca , res); } int main() { power[0] = 1; for (int i = 1 ; i < N ; ++ i) { power[i]=(LL)power[i - 1] * 26 % Q; } while (~scanf("%d" , &n)) { work(); } return 0; }