Balala Power!Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 2619 Accepted Submission(s): 547
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)
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
Sample Output
Source
|
1、设一个结构体 alph[26][100000]表示第i个字母,在第i位上的权重,当某一位的权重>=26时,就进位。
2、注意前导零的存在,字符串>1时,就记录一下第一个字符,这个字符就不能是0
3、把可以当作0的字符位置与0字符交换前,要把中间的数整体向左移动一位,保证其最大的特性。
4、我的代码写的真恶心。。。。我自己都不想看了
#include <iostream>
#include <string>
#include <string.h>
#include <algorithm>
#include <array>
#include <stdio.h>
using namespace std;
const int maxn = 100000;
const int mod = 1000000007;
struct Alph {
int bit[maxn + 10];
int value;
}alph[29];
bool cmp(Alph& a1, Alph& a2) {
for (int i = maxn; i >= 1; --i) {
if (a1.bit[i] == a2.bit[i])
continue;
if (a1.bit[i] > a2.bit[i])
return true;
else
return false;
}
return false;
}
long long p[maxn + 5];
int visited[27];
int main() {
//freopen("1.txt", "r", stdin);
int n;
p[0] = 1;
for (int i = 1; i <= maxn; ++i) {
p[i] = (p[i-1] * 26) % mod;
}
int ca = 1;
while (scanf("%d", &n) != EOF) {
memset(alph, 0, sizeof(alph));
memset(visited, 0, sizeof(visited));
for (int i = 0; i < 26; ++i) {
alph[i].value = i;
}
for (int i = 0; i < n; ++i) {
char str[maxn + 10];
scanf("%s", str);
long len = strlen(str);
if (len >= 1)
visited[str[0] - 'a'] = 1;
for (long i = len - 1; i >= 0; --i) {
alph[str[i] - 'a'].bit[len - i]++;
}
}
for (int i = 0; i < 26; ++i) {
int temp = 0;
for (int j = 1; j < maxn; ++j) {
temp = alph[i].bit[j] / 26;
alph[i].bit[j] %= 26;
alph[i].bit[j + 1] += temp;
}
}
sort(alph, alph + 26, cmp);
//cout << alph[0].bit[1] << endl;
//cout << static_cast<char>(alph[25].value + 'a') << endl;
if (visited[alph[25].value]) {
int pos = 25;
while (pos--) {
if (!visited[alph[pos].value]) {
break;
}
}
//cout << pos << endl;
Alph temp = alph[pos];
for (int i = pos; i < 25; ++i) {
alph[i] = alph[i + 1];
}
alph[25] = temp;
}
//cout << static_cast<char>(alph[23].value + 'a') << endl;
long long ans = 0;
for (int i = 0; i < 26; ++i) {
for (int j = 1; j <= maxn; ++j) {
ans = (ans + ((alph[i].bit[j] * (25 - i))* p[j-1]) % mod) % mod;
}
}
printf("Case #%d: %lld\n", ca++, ans);
//cout << ans << endl;
}
}
本文介绍了一个有趣的问题:如何通过调整字符映射来最大化一组字符串按26进制相加后的总和,并确保没有前导零。文章详细讨论了解决方案的四个关键步骤,包括使用结构体来跟踪字符权重、处理前导零限制、通过排序优化字符分配以及计算最终答案。

437

被折叠的 条评论
为什么被折叠?



