题目链接

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
1
a
2
aa
bb
3
a
ba
abc
 
Sample Output
Case #1: 25
Case #2: 1323
Case #3: 18221
 
题意:有n个小写字母组成的字符串,现在可以对'a'~'z'分别赋值,取0~25之间的一个数,但是每个数只能取一次,那么每个串就变成了一个26进制的数,现在求使这些数的和最大,输出这个最大的和,注意不能有前导零。
 
思路:对于每个字母先算出它的系数,对于‘ba’ 串,‘a’-->1,'b'-->26  。那么对于题中的数据‘a’-->1+1+26*26=678 ,'b'-->26+26=52 , 'c'-->1  所以对于这些串的和为sum=678a+52b+c,那么和明显对于系数大的字母赋较大的值,贪心的赋值即可。 但是由于这个串很长10^5 那么26^100000 早就爆 long long  了,所以不能这样算出他们的系数,然后比较系数大小了。我们可以定义一个数组a[26][100005],用来标示每个字母对应的几次方有多少个,当然对于值大于25的时候,向前进位,最后利用搜索从最高位向低位进行搜索,对于最高位时,值最大的说明该字符对应的系数肯定是最大的,如果有多个最大值,那么就需要第二高位,依次递推……
 
代码如下:
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
using namespace std;
typedef long long LL;
const int MAXN=1e5+;
const LL MOD = 1e9+;
char str[MAXN];
int k[][MAXN];
int mp[];
int ans[];
int temp;
int check(int a[])
{
int num=;
for(int i=;i<;i++)
num+=a[i];
return num;
}
int pp()
{
for(int i=;i<;i++)
if(mp[i]==) return i;
} void dfs(int dep,int o[])
{
if(dep<) return ;
int maxn=-;
int oo[] , ct=,pos=-;
memset(oo,,sizeof(oo));
for(int i=;i<=;i++)
{
if(maxn<k[i][dep]&&o[i]==)
maxn=k[i][dep];
}
for(int i=;i<=;i++)
{
if(maxn==k[i][dep]&&o[i]==)
{
oo[i]=;
o[i]=;
ct++;
pos=i;
}
}
if(ct==) return ;
if(ct==){
if(check(mp)==&&pos==pp()) ans[pos]=;
else ans[pos]=temp++;
mp[pos]=;
}
else if(dep==){
for(int i=;i<;i++)
{
if(maxn==k[i][dep]&&oo[i]==)
{
if(check(mp)==&&i==pp()) ans[i]=;
else ans[i]=temp++;
mp[i]=;
}
}
}
else dfs(dep-,oo);
if(check(o)>) dfs(dep,o);
}
LL Pow(LL a, LL b){
LL ans = ;
while(b){
if(b & ) ans = (ans * a) % MOD;
b>>=;
a = (a * a ) % MOD;
}
return ans;
} int main()
{
int T,n,L,l,cas=;
int one[];
while(scanf("%d",&n)!=-)
{
L=;
for(int i=;i<;i++) one[i]=,mp[i]=;
memset(k,,sizeof(k));
///memset(ans,0,sizeof(ans));
for(int i=;i<n;i++)
{
scanf("%s",str);
mp[str[]-'a']=;
l=strlen(str);
if(l>L) L=l;
for(int j=;j<l;j++) k[str[j]-'a'][l-j-]++;
}
for(int i=;i<;i++)
{
for(int j=;j<L+;j++)
{
k[i][j+]+=k[i][j]/;
k[i][j]=k[i][j]%;
}
}
temp=;
dfs(L+,one);
///for(int i=0;i<26;i++) cout<<ans[i]<<" "; cout<<endl;
LL res=0;
for(int i=;i<;i++)
{
for(int j=;j<L+;j++)
{
///res+=(25-ans[i])*Pow(26,j)*k[i][j];
res = (res+((-ans[i])*Pow(,j) % MOD)*k[i][j]%MOD)%MOD;
}
}
printf("Case #%d: %lld\n",cas++,res);
}
return ;
}

HDU 6034---Balala Power!(搜索+贪心)的更多相关文章

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

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

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

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

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

  4. hdu 6034 Balala Power!

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

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

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

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

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

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

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

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

  9. hdu 5335 Walk Out 搜索+贪心

    Walk Out Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total S ...

  10. 6034 Balala Power! (17多校)

    题目大意:给出的字符串,每个字符建立一种与0-25的对应关系.然后每个字符串看成是一个26进制的数.问能获得的数的总和的最大值.(最后对1e9+7取模). 题目思考:把每个字符的贡献值看做一个26进制 ...

随机推荐

  1. 微信小程序开发 -- 01

    微信小程序开发基础 -- 开发前的准备 缘由 1月9日张小龙微信小程序正式上线,因为微信,所以小程序从诞生开始就头戴巨大的光环,很多的团队,公司以及开发的个体都眼巴巴的盯着这个小程序.而那个时候我却在 ...

  2. MAC下解决eclipse卡顿或者运行慢的问题

    提示:假设你已经装了固态硬盘,并且有至少8Gb的内存.如果没有的话,带来的性能提升可能不大. 1.eclipse中加载的SDK数量过多会导致程序运行缓慢,解决方法删除plaforms下面用不到的SDK ...

  3. View Components as Tag Helpers,离在线模板编辑又进一步

    在asp.net core mvc中增加了ViewComponent(视图组件)的概念,视图组件有点类似部分视图,但是比部分视图功能更加强大,它更有点像一个控制器. 使用方法 1,定义类派生自View ...

  4. Disruptor——一种可替代有界队列完成并发线程间数据交换的高性能解决方案

    本文翻译自LMAX关于Disruptor的论文,同时加上一些自己的理解和标注.Disruptor是一个高效的线程间交换数据的基础组件,它使用栅栏(barrier)+序号(Sequencing)机制协调 ...

  5. [POJ 2115} C Looooops 题解(扩展欧几里德)

    题目描述 对于C的for(i=A ; i!=B ;i +=C)循环语句,给出A,B,C和k(k表示变量是在k进制下的无符号整数),判断循环次数,不能终止输出"FOREVER". 输 ...

  6. EF架构~Migration数据迁移的执行顺序

    回到目录 对于单个分支项目来说,只要你生成一个migration的版本,就会有一个时间戳文件的对应,而在update-database时,会从最小的时间开始,一直执行到当前版本的migration,而 ...

  7. Treasure Hunt

    Treasure Hunt time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  8. jquery中div悬浮嵌套按钮效果

    <div class="btn_sure_cai" style="margin-left: 0px;" onmouseover="show_hi ...

  9. Gist - ES6 Promise

    The concept of "Promise" Promise is used to asynchronous computations. Introduction " ...

  10. Angular 4 学习笔记 从入门到实战 打造在线竞拍网站 基础知识 快速入门 个人感悟

    最近搞到手了一部Angular4的视频教程,这几天正好有时间变学了一下,可以用来做一些前后端分离的网站,也可以直接去打包web app. 环境&版本信息声明 运行ng -v @angular/ ...