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. 解决Python安装模块出错 ImportError: No module named setuptools

    1.下载ez_setup.py文件 下载地址:https://bootstrap.pypa.io/ez_setup.py 百度地址:http://pan.baidu.com/s/1jIgMO9w 2. ...

  2. linux 软中断过高性能优化案例

    案例如下: 发现cpu0上的软中断高达50%

  3. linux重启服务的脚本命令

    最近做网站测试,每次测试完成都要重启服务,为此写了一个简单的shell脚本 linux服务重启shell脚本示例 2014年12月18日 linux服务重启脚本,如何实现linux服务的定时重启,可以 ...

  4. 计划任务_crontab

    1. crontab原理和使用 Cron 实际上是两个独立的程序.Cron damon, 或者叫做cron ,crond 它是伴随系统一起启动的常驻程序 来检查是否cron 在系统上运行, 用ps 命 ...

  5. POJ3076 Sudoku 舞蹈链 DLX

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目(传送门) 题意概括 给出一个残缺的16*16数独,求解. 题解 DLX + 矩阵构建  (两个传送门) 学完这个之后,再 ...

  6. python 操作mongo

    1. 导包: import pymongo 2. 建立连接 client = pymongo.MongoClient("127.0.0.1",27017) 3. 获取数据库 db ...

  7. Shell学习之环境变量配置文件(三)

    Shell学习之环境变量配置文件 目录 环境变量配置文件简介 环境变量配置文件作用 其他配置文件和登录信息 环境变量配置文件简介 环境变量配置文件简介 环境变量配置文件中主要是定义对系统操作环境生效的 ...

  8. cmd使用notepad++为打开方式打开文件

    想放一个txt进入vstart中,但是又不想用系统自带的记事本打开,想在vstart中双击时使用notepad++打开. cmd命令如下: "D:\notepad++\notepad++.e ...

  9. 前端之HTML(一)

    最近学到前端的一些知识,感觉挺有意思的.总结一下常用的知识.这些只是最简单的东西. 一 HTML,CSS,JS的关系 一个基本的网站包含很多网页,一个网页又有html,css,js组成. html 是 ...

  10. #6164. 「美团 CodeM 初赛 Round A」数列互质-莫队

    #6164. 「美团 CodeM 初赛 Round A」数列互质 思路 : 对这个题来言,莫队可以 n*根号n 离线处理出各个数出现个的次数 ,同时可以得到每个次数出现的次数 , 但是还要处理有多少 ...