您的位置:首页 > 编程语言

2017 Multi-University Training Contest - Team 1 B - Balala Power! (思维代码能力,贪心)

2017-07-26 21:51 381 查看


Talented Mr.Tang has nn strings
consisting of only lower case characters. He wants to charge them with Balala Power (he could change each character ranged from a to zinto 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 2626 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+7109+7.

InputThe input contains multiple test cases. 

For each test case, the first line contains one positive integers nn,
the number of strings. (1≤n≤100000)(1≤n≤100000) 

Each of the next nn lines
contains a string sisi consisting
of only lower case letters.(1≤|si|≤100000,∑|si|≤106)(1≤|si|≤100000,∑|si|≤106) 

OutputFor each test case, output " Case #xx: yy"
in one line (without quotes), where xxindicates
the case number starting from 11 and yy 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个字符串,你可以对于每个字母进行赋值,0~25,不允许字符串的首位为0,要求算出26进制的最大和

解题思路:对于每个字母所出现的位数进行统计,然后对26个字母进行排序,优先让那些出现在高位,出现次数多的尽可能赋予大数,同时判一下前导0

这道题在进行排序和整合的时候超级麻烦,都是基本上思路上对了,但是在代码实现上直接GG

#include<iostream>
#include<cstdio>
#include<stdio.h>
#include<cstring>
#include<cstdio>
#include<climits>
#include<cmath>
#include<vector>
#include <bitset>
#include<algorithm>
#include <queue>
#include<map>
#define inf 9999999;
using namespace std;
typedef long long LL;

int b[30],L;
bool check[30];
LL a[30][100010];
LL pt=1e9+7;
LL pows[100010];
LL sum[100010],ans;
bool cmp(int x,int y)
{
for(int i=L-1;i>=0;i--)
{
if(a[x][i]!=a[y][i])
return a[x][i]<a[y][i];
}
return 0;
}

int main()
{
int i,j,len,n;
int k=0;
string str;
pows[0]=1;
for(i=1;i<100005;i++)
{
pows[i]=pows[i-1]*26%pt;
}
while(cin>>n)
{
//	int L;
L=0;
memset(sum,0,sizeof(sum));
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
memset(check,0,sizeof(check));
for(i=1;i<=n;i++)
{
cin>>str;
len=str.length();
for(j=0;j<len;j++)
{
if(len>1)
check[str[0]-'a']=1;
a[str[j]-'a'][len-j-1]++;
//cout<<pows[len-j-1]<<endl;
sum[str[j]-'a']=(sum[str[j]-'a']+pows[len-j-1])%pt;
}
L=max(L,len);
}
for(i=0;i<26;i++)
{
for(j=0;j<L;j++)
{
a[i][j+1]+=a[i][j]/26;
a[i][j]%=26;
}
while(a[i][L])//这是个坑考虑到最高位置满26后要上进一位
{
a[i][L+1]+=a[i][L]/26;
a[i][L++]%=26;//这里L++的原因是这个位置由于原先L的位置满26进位,所以要考虑进去,同理在sort中也是
}
b[i]=i;
}
sort(b,b+26,cmp);
int cnt=-1;
for(i=0;i<26;i++)
{
if(check[b[i]]==0)
{
cnt=b[i];
break;
}
}
ans=0;
int x=25;
for(i=25;i>=0;i--)
{
if(b[i]!=cnt)
{
ans=(ans+(x--)*sum[b[i]])%pt;
}
}
cout<<"Case #"<<++k<<": "<<ans<<endl;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐