Balala Power!
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 879 Accepted Submission(s): 146
Total Submission(s): 879 Accepted Submission(s): 146
Problem Description
![](https://i-blog.csdnimg.cn/blog_migrate/206c742d60bb80a1bf080aa3f1c098a0.jpeg)
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 froma to z into each number ranged from 0 to25, 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
题意:
n串字母,总长度不超过1e6,让你给出a-z到0-25的映射,使26进制的字符串对应的数字之和最大。
解题思路:
每个字母所在位置对应权值加和,肯定存不下。
但我们只需要26个字母对应值之间的关系即可,开一个数组a[i][j]分别记录字母i在j这个位置上出现了多少次,对于大于26的值进位,这样我们就得到了26个字母对应的值对应的系数,按照字典序比较的方式去排序即可。
但是要注意,题目虽然说一定有一个字母没有出现在最高位过,保证了一定存在合法的映射,(合法即不能出现前导零),但是排序后的序列可能将出现在最高位过的字母排到最后一位,赋值为0就非法了。所以这种情况需要特判,然后从后往前找第一个没出现在最高位的字母替换到最后,前面依次迁移。
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
const int MAXN = 1e5 + 7;
const long long mod = 1e9 + 7;
int n, maxj;
struct node
{
int id;
int num[MAXN] = {0};
bool operator < (const node &a)const
{
for (int j = maxj; j >= 0; --j)
if (num[j] != a.num[j])
{
return num[j] > a.num[j];
}
return 0;
}
} p[30];
string s[MAXN];
long long num[30];
long long k[MAXN];
int book[30];
int main()
{
ios::sync_with_stdio(false);
k[0] = 1;
for (int i = 1 ; i <= 100000; ++i)
{
k[i] = (k[i - 1] * 26) % mod;
}
int ca = 0;
maxj = 0;
while (cin >> n)
{
for (int i = 0; i < 26; ++i)
{
book[i] = 0;
for (int j = 0; j <= maxj; j++)
{
p[i].num[j] = 0;
}
p[i].id = i;
}
for (int i = 0; i < n; ++i)
{
cin >> s[i];
int l = s[i].size();
for (int ii = l - 1, j = 0; ii >= 0; --ii, ++j)
{
if (ii == 0)
{
book[s[i][ii] - 'a'] = 1;
}
int t = s[i][ii] - 'a';
p[t].num[j]++;
}
}
maxj = 0;
for (int i = 0; i < 26; ++i)
{
for (int j = 0; j <= 100000; ++j)
{
p[i].num[j + 1] += p[i].num[j] / 26;
p[i].num[j] %= 26;
if (p[i].num[j] > 0)
{
maxj = max(j, maxj);
}
}
}
sort(p, p + 26);
int i;
if (book[p[25].id])
{
for (i = 25; i >= 0; i--)
if (book[p[i].id] == 0)
{
break;
}
p[26] = p[i];
for (; i < 26; i++)
{
p[i] = p[i + 1];
}
}
for (int i = 25; i >= 0; --i)
{
num[p[25 - i].id] = i;
}
long long ans = 0;
for (int i = 0; i < n; ++i)
{
int l = s[i].size();
for (int j = 0; j < l; ++j)
{
ans = (ans + num[s[i][j] - 'a'] * k[l - 1 - j] % mod) % mod;
}
}
cout << "Case #" << ++ca << ": " << ans << endl;
}
return 0;
}