【多校训练】hdu 6034 Balala Power!
2017-08-02 15:16
381 查看
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个字符串,给26个字母赋值0-25,使字符串的和最大,不能有前导0,求最大值。
思路:
可以统计下每个字母的权重,权重最大的字母赋值为25,,以此类推。要让非首字母且权重相对较小的字母为0。
//
// main.cpp
// 1002
//
// Created by zc on 2017/7/25.
// Copyright © 2017年 zc. All rights reserved.
//
#include <iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#define ll long long
using namespace std;
const ll MOD=1e9+7;
char s[120000];
int a[27][120000];
int b[27],c[27],e[27];
ll d[120000];
bool cmp(int x,int y)
{
for(int i=110000;i>=0;i--)
{
if(a[x][i]!=a[y][i]) return a[x][i]<a[y][i];
}
return 0;
}
int main(int argc, const char * argv[]) {
int n,kase=0;
d[1]=1;
for(int i=2;i<=110000;i++) d[i]=(d[i-1]*26)%MOD;
while(~scanf("%d",&n))
{
memset(a,0,sizeof(a));
memset(e,0,sizeof(e));
for(int i=0;i<n;i++)
{
scanf("%s",s);
int len=strlen(s);
for(int j=len-1;j>=0;j--)
{
a[s[j]-'a'][len-j]++;
if(j==0) e[s[j]-'a']=1;
}
}
for(int i=0;i<26;i++)
{
for(int j=1;j<=110000;j++)
{
if(a[i][j]>26)
{
a[i][j+1]+=a[i][j]/26;
a[i][j]%=26;
}
}
}
for(int i=0;i<26;i++) b[i]=i;
sort(b,b+26,cmp);
if(e[b[0]]==1)
{
int t=1;
while(t<26&&e[b[t]]==1) t++;
int tt=b[t];
for(int i=t-1;i>=0;i--) b[i+1]=b[i];
b[0]=tt;
}
for(int i=0;i<26;i++) c[b[i]]=i;
ll ans=0;
for(int i=0;i<=26;i++)
{
for(int j=1;j<=110000;j++)
{
ans=(ans+c[i]*a[i][j]*d[j])%MOD;
}
}
printf("Case #%d: %lld\n",++kase,ans);
}
}
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个字符串,给26个字母赋值0-25,使字符串的和最大,不能有前导0,求最大值。
思路:
可以统计下每个字母的权重,权重最大的字母赋值为25,,以此类推。要让非首字母且权重相对较小的字母为0。
//
// main.cpp
// 1002
//
// Created by zc on 2017/7/25.
// Copyright © 2017年 zc. All rights reserved.
//
#include <iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#define ll long long
using namespace std;
const ll MOD=1e9+7;
char s[120000];
int a[27][120000];
int b[27],c[27],e[27];
ll d[120000];
bool cmp(int x,int y)
{
for(int i=110000;i>=0;i--)
{
if(a[x][i]!=a[y][i]) return a[x][i]<a[y][i];
}
return 0;
}
int main(int argc, const char * argv[]) {
int n,kase=0;
d[1]=1;
for(int i=2;i<=110000;i++) d[i]=(d[i-1]*26)%MOD;
while(~scanf("%d",&n))
{
memset(a,0,sizeof(a));
memset(e,0,sizeof(e));
for(int i=0;i<n;i++)
{
scanf("%s",s);
int len=strlen(s);
for(int j=len-1;j>=0;j--)
{
a[s[j]-'a'][len-j]++;
if(j==0) e[s[j]-'a']=1;
}
}
for(int i=0;i<26;i++)
{
for(int j=1;j<=110000;j++)
{
if(a[i][j]>26)
{
a[i][j+1]+=a[i][j]/26;
a[i][j]%=26;
}
}
}
for(int i=0;i<26;i++) b[i]=i;
sort(b,b+26,cmp);
if(e[b[0]]==1)
{
int t=1;
while(t<26&&e[b[t]]==1) t++;
int tt=b[t];
for(int i=t-1;i>=0;i--) b[i+1]=b[i];
b[0]=tt;
}
for(int i=0;i<26;i++) c[b[i]]=i;
ll ans=0;
for(int i=0;i<=26;i++)
{
for(int j=1;j<=110000;j++)
{
ans=(ans+c[i]*a[i][j]*d[j])%MOD;
}
}
printf("Case #%d: %lld\n",++kase,ans);
}
}
相关文章推荐
- 2017 多校训练第一场 HDU 6034 Balala Power!
- (2017多校训练第一场)HDU - 6034 Balala Power! 贪心
- 2017 杭电多校联赛 1002 Balala Power!(数字替换字母)HDU 6034
- Balala Power! HDU - 6034 多校1
- HDU 6034-(2017多校第一场 Balala Power!)(贪心)
- HDU 2017 多校联合训练赛1 1002 6034 Balala Power 排序
- HDU 6034 Balala Power! 2017多校1.2
- 17 多校 - 1 - 1002 - Balala Power! (HDU 6034)
- HDU 6034 Balala Power!(多校1)
- Balala Power! HDU - 6034 贪心
- hdu 6034 Balala Power!(贪心)( 2017 Multi-University Training Contest - Team 1 )(无耻之sort)
- 2017 多校训练 1002 Balala Power!
- hdu-6034-Balala Power!
- HDU 6034 Balala Power!(贪心)
- HDU-2017 多校训练赛1-1002-Balala Power!
- hdu 6034 Balala Power!(贪心)( 2017 Multi-University Training Contest - Team 1 )(无耻之sort)
- HDU 6034 Balala Power! (贪心+坑题)
- HDU6034 Balala Power!(贪心,坑!!!)(2017 HDU多校联赛第一场)
- HDU 6034 Balala Power!(贪心)
- hdu 6034 Balala Power!(贪心)( 2017 Multi-University Training Contest - Team 1 )(无耻之sort)