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; }
相关文章推荐
- hdu 6034 Balala Power!(贪心)( 2017 Multi-University Training Contest - Team 1 )(无耻之sort)
- 2017 Multi-University Training Contest - Team 1 1002&&HDU 6034 Balala Power!【字符串,贪心+排序】
- HDU 3348 coins 贪心 最少/最多硬币问题
- c语言打印字符乱码和char *字符循环赋值时“段错误 (核心已转储)”问题解决
- 一个字符数组赋值的问题
- HDU 5501(一类贪心问题)
- HDU 6034 Balala Power! (贪心+坑题)
- HDU-6034 Balala Power! - 2017 Multi-University Training Contest - Team 1(贪心)
- hdu 6034 Balala Power!(贪心)( 2017 Multi-University Training Contest - Team 1 )(无耻之sort)
- HDU 6034 Balala Power!(贪心)
- HDU-1789 Doing Homework again 贪心问题 有时间限制的最小化惩罚问题
- hdu 6034 Balala Power!(贪心)( 2017 Multi-University Training Contest - Team 1 )(无耻之sort)
- (2017多校训练第一场)HDU - 6034 Balala Power! 贪心
- HDU 2570 迷瘴(贪心 过程处理不好会导致精度问题)
- HDU 6034 Balala Power!【贪心】
- hdu 6034 Balala Power!(贪心)( 2017 Multi-University Training Contest - Team 1 )(无耻之sort)
- 含软连字符的字符串计算显示长度的问题
- HDU 4802 && HDU 4803 贪心,高精 && HDU 4804 轮廓线dp && HDU 4805 计算几何 && HDU 4811 (13南京区域赛现场赛 题目重演A,B,C,D,J)
- c语言中,字符数组与字符串赋值问题
- 计算 24 点是一种扑克牌益智游戏,随机抽出 4 张扑克牌,通过加 (+) ,减 (-) ,乘 ( * ), 除 (/) 四种运算法则计算得到整数 24 ,本问题中,扑克牌通过如下字符或者字符串表示,其中,小写 joker 表示小王,大写 JOKER 表示大王: