B - Balala Power!

题目链接

http://acm.hdu.edu.cn/showproblem.php?pid=6034

题面描述

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.

输入

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, 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.

样例输入

1

a

2

aa

bb

3

a

ba

abc

样例输出

Case #1: 25

Case #2: 1323

Case #3: 18221

题意

给你n个由26个字母组成的字符串,你现在要给每个字母用[0,25]赋值,不能要求有前导0,每个值对应一个字母.

要求使得字符串组成的数字和最大。

题解

直接算每个字母的贡献,然后赋值就行,把0给最小的合法的字母即可。

代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+7;
const int mod = 1e9+7;
struct node{
int len[maxn];
int w;
int v;
int L;
}p[26];
int n;
string s[maxn];
int flag[26];
int cas = 0;
bool cmp(node A,node B){
if(A.L!=B.L)return A.L<B.L;
for(int i=A.L;i>=0;i--){
if(A.len[i]==B.len[i])continue;
return A.len[i]<B.len[i];
}
return 0;
}
int main(){
while(cin>>n){
cas++;
memset(flag,0,sizeof(flag));
for(int i=0;i<26;i++){
for(int j=0;j<=p[i].L;j++){
p[i].len[j]=0;
}
}
for(int i=0;i<26;i++){
p[i].v=0;
p[i].w=i;
p[i].L=-1;
}
for(int i=0;i<n;i++){
cin>>s[i];
if(s[i].size()>1){
flag[s[i][0]-'a']++;
}
reverse(s[i].begin(),s[i].end());
for(int j=0;j<s[i].size();j++){
p[s[i][j]-'a'].len[j]++;
}
}
for(int i=0;i<26;i++){
for(int j=0;j<maxn-1;j++){
if(p[i].len[j]>=26){
int d = p[i].len[j]/26;
p[i].len[j]%=26;
p[i].len[j+1]+=d;
}
if(p[i].len[j]>0){
p[i].L=max(p[i].L,j);
}
}
}
sort(p,p+26,cmp);
for(int i=0;i<26;i++){
p[i].v=i;
}
for(int i=0;i<26;i++){
if(flag[p[i].w]&&p[i].v==0){
swap(p[i].v,p[i+1].v);
}else break;
}
long long ans = 0;
long long value[26];
for(int i=0;i<26;i++){
value[p[i].w]=p[i].v;
}
for(int i=0;i<n;i++){
long long now = 1;
for(int j=0;j<s[i].size();j++){
ans=(ans+(value[s[i][j]-'a']*now)%mod)%mod;
now=(now*26)%mod;
}
}
cout<<"Case #"<<cas<<": "<<ans<<endl;
}
}

hdu 6034 B - Balala Power! 贪心的更多相关文章

  1. HDU 6034 Balala Power! (贪心+坑题)

    题意:给定一个 n 个字符串,然后问你怎么给 a-z赋值0-25,使得给定的字符串看成26进制得到的和最大,并且不能出现前导0. 析:一个很恶心的题目,细节有点多,首先是思路,给定个字符一个权值,然后 ...

  2. 2017 Multi-University Training Contest - Team 1 1002&&HDU 6034 Balala Power!【字符串,贪心+排序】

    Balala Power! Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)T ...

  3. HDU 6034 Balala Power!(贪心+排序)

    Balala Power! Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) ...

  4. HDU 6034 - Balala Power! | 2017 Multi-University Training Contest 1

    /* HDU 6034 - Balala Power! [ 大数进位,贪心 ] 题意: 给一组字符串(小写英文字母),将上面的字符串考虑成26进制数,每个字母分配一个权值,问这组数字加起来的和最大是多 ...

  5. HDU 6034 17多校1 Balala Power!(思维 排序)

    Problem Description Talented Mr.Tang has n strings consisting of only lower case characters. He want ...

  6. hdu 6034 Balala Power!

    Balala Power! Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)T ...

  7. 2017ACM暑期多校联合训练 - Team 1 1002 HDU 6034 Balala Power! (字符串处理)

    题目链接 Problem Description Talented Mr.Tang has n strings consisting of only lower case characters. He ...

  8. HDU 6034 Balala Power!【排序/进制思维】

    Balala Power![排序/进制思维] Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java ...

  9. HDU 6034 Balala Power! —— Multi-University Training 1

    Talented Mr.Tang has nn strings consisting of only lower case characters. He wants to charge them wi ...

随机推荐

  1. C#学习-析构函数

    析构函数用于在类销毁之前释放类实例所使用的托管和非托管资源. 对于C#应用程序所创建的大多数对象,可以依靠.NET Framework的垃圾回收器(GC)来隐式地执行内存管理任务. 但是,若创建封装了 ...

  2. [转] 对Array.prototype.slice.call()方法的理解

    在看别人代码时,发现有这么个写法:[].slice.call(arguments, 0),这到底是什么意思呢? 1.基础 1)slice() 方法可从已有的数组中返回选定的元素. start:必需.规 ...

  3. haoi2018

    题解: 题目相对其他省难一点 不过弱省省选知识点都这么集中的么.. 4道数学题... 1.[HAOI2018]奇怪的背包 这题考场做就gg了... 其实我想到了那个性质.. 就是这个一定要是gcd的倍 ...

  4. 1个汉字在UTF-8编码占3个字节

    http://blog.csdn.net/ns_code/article/details/14162087 http://www.ruanyifeng.com/blog/2007/10/ascii_u ...

  5. hibnet 自动生成表格

    在pom文件中添加相关的依赖 在application.yml文件中添加配置信息 新建实体类Boy 项目运行,mysql就产生了一个boy表. 需要注意的是:当配置文件ddl-auto: create ...

  6. 分布式配置hadoop2.5.0 2.6.x

    1. sudo vim /etc/hostname 在master的机器上,改成     master 在slave上写  slave01,02,03...... 配置好后重启. 2. sudo vi ...

  7. python--模拟蜂窝网(https)登陆总结

    #用户名密码登陆 1.寻找登陆请求(此处可以故意输错用户名密码,目的是为了能够看清楚重定向的地址) 发现: 点击登陆时,请求了 ①.post302:https://passport.mafengwo. ...

  8. ibatis 多种传参方式

    1,在公司项目yuda遇到的传入in语句,如果直接拼接in语句:in (....),sqlmap中使用#...#输出是不行的. 为需要使用: 第三种:in后面的数据确定,使用string传入      ...

  9. 065 updateStateByKey的函数API

    一:使用场景 1.应用场景 数据的累加 一段时间内的数据的累加 2.说明 每个批次都输出自己批次的数据, 这个时候,可以使用这个API,使得他们之间产生联系. 3.说明2 在累加器的时候,起到的效果和 ...

  10. day7 [id],[is],编码

    老师的笔记: 字典:dic = {'name':'alex'} 1,增 dic['k'] = 'v' 有键值对,则覆盖 setdefault 有键值对,不添加 dic.setdefault('k1', ...