Balala Power!

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 1411    Accepted Submission(s): 239

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

题目链接:HDU 6034

嗯今天多校第一场的一道题由于不知道怎么地把cmp函数写炸了,最后就没写出来。很显然肯定是把最大的系数留给贡献最大的字母,因此贪心地把最大的系数从25-0依次赋值,然后再选取一个最小贡献且不是开头字母的来牺牲一下获得系数0,其他的字母就均不为0了。然后预处理一下26的指数即可,贡献怎么算?一开始按对贡献取模之后排序,后来发现取模之后的排肯定不对,然后用一个结构体记录每一个字母和它出现的位数以及次数,然后手动模拟进位一下,否则比如第0位出现100次肯定比第1位出现1次贡献大(此处要注意可能会进位到maxlen处,比较的时候要多一位,否则WA),最后写一个比较奇怪的cmp函数就可以了,这样就可以正常地排序比较。

代码:

#include <stdio.h>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <bitset>
#include <string>
#include <stack>
#include <cmath>
#include <queue>
#include <set>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
#define fin(name) freopen(name,"r",stdin)
#define fout(name) freopen(name,"w",stdout)
#define CLR(arr,val) memset(arr,val,sizeof(arr))
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
typedef pair<int, int> pii;
typedef long long LL;
const double PI = acos(-1.0);
const int N = 1e5 + 7;
const LL mod = 1e9 + 7;
LL fac[N];
char s[N];
int ishead[30], maxlen; struct info
{
char c;
LL val;
int f[N];
void init()
{
val = 0LL;
CLR(f, 0);
}
void cal()
{
for (int i = 0; i < maxlen; ++i)
{
if (f[i] >= 26)
{
f[i + 1] += f[i] / 26;
f[i] %= 26;
}
}
}
bool operator<(const info &rhs)const
{
for (int i = maxlen; i >= 0; --i)
{
if (f[i] != rhs.f[i])
return f[i] < rhs.f[i];
}
}
};
info arr[26]; void init()
{
CLR(ishead, 0);
maxlen = 0;
for (int i = 0; i < 26; ++i)
arr[i].init();
}
int main(void)
{
int i, j, n;
fac[0] = 1LL;
for (i = 1; i < N; ++i)
fac[i] = fac[i - 1] * 26LL % mod;
int q = 1;
while (~scanf("%d", &n))
{
init();
for (i = 0; i < n; ++i)
{
scanf("%s", s);
int len = strlen(s);
if (len > 1)
ishead[s[0] - 'a'] = 1;
if (len > maxlen)
maxlen = len;
for (j = 0; j < len; ++j)
{
int id = s[j] - 'a';
arr[id].val = (arr[id].val + fac[len - j - 1]) % mod;
++arr[id].f[len - j - 1];
}
}
for (i = 0; i < 26; ++i)
{
arr[i].c = 'a' + i;
arr[i].cal();
}
int sw = 0;
for (i = 0; i < 26; ++i)
{
if (!ishead[arr[i].c - 'a'] && arr[i] < arr[sw])
sw = i;
}
swap(arr[0], arr[sw]);
sort(arr + 1, arr + 26);
LL ans = 0;
for (i = 0; i < 26; ++i)
{
ans = ans + (arr[i].val * (LL)(i)) % mod;
ans %= mod;
}
printf("Case #%d: %I64d\n", q++, ans);
}
return 0;
}

HDU 6034 Balala Power!(贪心+排序)的更多相关文章

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

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

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

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

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

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

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

  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/Others)T ...

  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 B - Balala Power! 贪心

    B - Balala Power! 题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=6034 题面描述 Talented Mr.Tang has n st ...

  9. 6034 Balala Power! (17多校)

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

随机推荐

  1. jquery 操作ajax 相关方法

    jQuery.get() 使用一个HTTP GET 请求从服务器加载数据. jQuery.get(url [,data] [,success(data,textStatus,jqXHR)] [dtaT ...

  2. 图的m着色

    图的m着色 #include <bits/stdc++.h> using namespace std; int n, k, m, ans; struct node{ int m, colo ...

  3. LNMP+HAProxy+Keepalived负载均衡 - LNMP基础环境准备

    环境版本说明: 服务器系统:CentOS 7.5: ``` cat /etc/redhat-release CentOS Linux release 7.5.1804 (Core) # 输出结果 `` ...

  4. php-5.6.26源代码 - 扩展模块的种类,扩展模块的执行埋点

    模块种类(两种) 类型一:zend的模块:(类似zend_extension=test.so) 识别方法: php.ini中以zend_extension开头的配置,如zend_extension=t ...

  5. 中缀表达式转后缀表达式(Python实现)

    中缀表达式转后缀表达式 中缀表达式转后缀表达式的规则: 1.遇到操作数,直接输出: 2.栈为空时,遇到运算符,入栈: 3.遇到左括号,将其入栈: 4.遇到右括号,执行出栈操作,并将出栈的元素输出,直到 ...

  6. iar注释快捷键

    选中多行后注释快捷键:Ctrl+K 取消多行注释快捷键:Ctrl+Shift+K

  7. CSS的z-index & 绝对定位与相对定位

    1.在有些情况下,需要仔细地控制元素在网页中堆叠顺序.z-index样式属性让你能够设置元素的堆叠顺序. 堆叠元素时,z-index值较大的元素在z-index值较小的下面. 2.z-index值仅在 ...

  8. 图的深度优先遍历&广度优先遍历

    1.什么是图的搜索? 指从一个指定顶点可以到达哪些顶点   2.无向完全图和有向完全图 将具有n(n-1)/2条边的无向图称为无向完全图(完全图就是任意两个顶点都存在边). 将具有n(n-1)条边的有 ...

  9. DOS程序员手册(十三)

    744页 在DPMI 1.0下,系统会修改并重新装载所有含选择符的段寄存器,并且将所有 含有要释放的选择符的寄存器清空为0. 客户程序绝不能修改或释放该功能分配的任何描述符.Int 31h.功能010 ...

  10. 梆梆加固还原DEX文件

    0x01 先说总结: 参照https://www.cnblogs.com/jiaoxiake/p/6818786.html 最后说的步骤, 参考:https://www.52pojie.cn/thre ...