Balala Power!
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 3437 Accepted Submission(s): 800
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
解题思路:计算出每个字符在每一位出现的次数,然后根据每个字符在每位出现的次数进行排序,最高位出现最多的必然最大,然后一个个排下来即可,前导零只要一开始做一下标记,然后不能为零的就和前面的进行交换即可(这题还要注意进位,即一次统计出来的并不是真正的每个字符出现的次数,还会存在进位的影响,不过最后进位后若字符串长度变长了可以不用继续进行进位,因为最高位不会再出现影响)
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <map>
#include <cmath>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <functional>
using namespace std;
#define LL long long
const int INF = 0x3f3f3f3f;
const LL mod = 1e9 + 7;
int n, ma, vis[30], ans[30];
string s[100009];
struct node
{
int id, sum[100009];
friend bool operator <(const node &a, const node &b)
{
for (int i = ma-1; i>=0; i--)
if (a.sum[i] != b.sum[i]) return a.sum[i]>b.sum[i];
}
} x[30];
int main()
{
int cas = 0;
while (~scanf("%d", &n))
{
ma = -1;
memset(vis, 0, sizeof vis);
for (int i = 1; i <= n; i++)
{
cin >> s[i], ma = max(ma, (int)s[i].length());
if (s[i].length() != 1) vis[s[i][0] - 'a' + 1] = 1;
}
memset(x, 0, sizeof x);
for (int i = 1; i <= n; i++)
{
int len = s[i].length();
for (int j = len-1; j>=0; j--)
x[s[i][j] - 'a' + 1].sum[len-1-j]++, x[s[i][j] - 'a' + 1].id = s[i][j] - 'a' + 1;
}
for (int i = 1; i < 27; i++)
{
for (int j = 0; j <ma; j++)
{
if (x[i].sum[j] >= 26)
{
x[i].sum[j + 1] += x[i].sum[j] / 26;
x[i].sum[j] %= 26;
}
}
}
for(int i=1;i<27;i++)
if(x[i].sum[ma]) {ma++;break;}
sort(x + 1, x + 27);
int cnt = 25;
for (int i = 1; i <= 26; i++)
{
ans[x[i].id] = cnt--;
if (i == 26)
{
int k = i;
while (vis[x[k].id])
{
swap(ans[x[k].id], ans[x[k - 1].id]);
k--;
}
}
}
LL ans1 = 0;
for (int i = 1; i <= n; i++)
{
int len = s[i].length();
LL sum = 0;
for (int j = 0; j<len; j++)
sum = (sum * 26 + ans[s[i][j] - 'a' + 1]) % mod;
ans1 += sum;
ans1 %= mod;
}
printf("Case #%d: %lld\n", ++cas, ans1);
}
return 0;
}