您的位置:首页 > 其它

HDU 6034 字符贪心赋值计算问题

2017-08-02 19:43 274 查看


Balala Power!

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)

Total Submission(s): 5274    Accepted Submission(s): 1332


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 10^9 + 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 \leq n \leq 100000)

Each of the next n lines contains a string s_i consisting of only lower case letters. (1
\leq |s_i| \leq 100000, \sum{|s_i|} \leq 10^6)

 

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

 

题意:输入多串字符串,只有小写字母,可以给每个字母赋一个从 0到25 的值,要求使所有串的值加起来最大,不难看出它意思就是把字母换成26进制的数

那么对于第二组数据的aa赋值25给a,24给b,那么aa + bb就是25 * 26 + 25 + 24 * 26 + 24 = 1323

思路:对于每个字母,统计出它所在第几位还有在那位出现的个数,全部统计好后进行一次排序,根据位置和个数排序,之后就对数值进行计算统计就好了

多校联合的一道题,当时比赛的时候敲了很久一wa,大佬也一直wa,最后结束过后才知道是wa在没有进位上了 。这里所谓的进位是指,你对某个位置上的一个字符统计时,如果这个字母在这一位置上出现的个数达到26,那么应该把它向上进一位,而不是一直加下去,因为那样在后面计算的时候,可能会爆longlong,没来的及就爆了的话,答案当然错了。

还有作为开头的字母,除非是只有单个,否则不能赋0

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<string>
#include<algorithm>
#include<map>
#include<math.h>
#define ll long long
#define MOD 1000000007
using namespace std;

struct Node{
int n;
string ch;
}node[100005];

int cmp(Node a,Node b){
return a.n > b.n;
}

struct Node2{
int a,b;
}num[300];

int cmp2(Node2 x,Node2 y){
return x.a > x.b;
}
int tv[100005][300];
int main(){
int m;
int Case = 1;
int k = 25;
int ans = 0;
int flag;

map<char,int>val;
while(scanf("%d",&m) != EOF){
k = 25;
ans = 0;
flag = 0;
val.clear();
memset(tv,0,sizeof(tv));
int theCnt = 0;
for(int i = 1;i <= m;i++){
cin>>node[i].ch;
int cnt = 0;
for(int j = node[i].ch.size() - 1;j >= 0;j--){
tv[++cnt][node[i].ch[j] - 'a']++;
if(cnt > theCnt)
theCnt = cnt;
}
node[i].n = node[i].ch.size();
}
for(int i = theCnt;i >= 1;i--){
if(flag == 26)
break;
int maxx = 0;
int u;
memset(num,0,sizeof(num));
int x = 0;

for(int j = 0;j <= 25;j++){
if(tv[i][j] > 0){
num[x].a = tv[i][j];
num[x].b = j;
x++;
}
}
sort(num,num + x,cmp2);
for(int j = 0;j < x;j++){
if(!val[num[j].b + 'a']){
val[num[j].b + 'a'] = k--;
flag++;
}
}
}
for(int i = 1;i <= m;i++){
int o = node[i].ch.size() - 1;
for(int j = 0;j < node[i].ch.size();j++){
ans += (val[node[i].ch[j]] * ((int)pow(26,o--) % MOD)) % MOD;
ans %= MOD;
}
}
printf("Case #%d: %d\n",Case++,ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐