HDU 6034---Balala Power!(搜索+贪心)

时间:2021-11-29 21:18:23

题目链接

Problem Description
HDU  6034---Balala Power!(搜索+贪心)
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 ;
}