HDU 6034-(2017多校第一场 Balala Power!)(贪心)

本文介绍了一个字符串求和问题的解决方案,通过记录每个字母在不同位置的出现次数,并采用贪心策略实现最优赋值,最终计算出字符串在26进制下的最大总和。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Balala Power!

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


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. (1n100000)

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 #xy" 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

//贪心,记录每个字母在每个位置出现的次数, (注意:满26要进位)
//记录开头字母,先判定应该给哪个字母赋值0
//最后给越早越多出现的字母给最大,依次赋值,最后计算总和
//菜鸡我想不到其他优化,1800ms卡过.........

#include <stdio.h>
#include <map>
#include <iostream>
#include <string.h>
#include <algorithm>
#define maxn 100000+50
#define inf 0x3f3f3f3f
#define ll long long
using namespace std;

ll mod = 1e9+7;
char s[maxn];
ll mp[maxn][26];
ll vis[maxn];
bool jilu[26];
ll mm[maxn];

struct Node
{
	int x;
	int num[maxn];
}node[26];

int cmp(const Node &a, const Node &b)
{
	for(int i=0; i<maxn; i++)
	{
		if(a.num[i] != b.num[i])
			return a.num[i] > b.num[i];
	}
	return a.num[maxn-1] > b.num[maxn-1];
}

int main()
{
	int n, kk=0;
	while(~scanf("%d", &n))
	{
		kk++;
		int zero = 0;
		ll h = 25, sum = 0;
        memset(mp, 0, sizeof(mp));
		memset(vis, 0, sizeof(vis));
		memset(jilu, 0, sizeof(jilu));
		memset(mm, 0, sizeof(mm));
		for(int i=0; i<26; i++)
        {
            node[i].x = i;
            memset(node[i].num, 0, sizeof(node[i].num));
        }
		for(int i=0; i<n; i++)
		{
			scanf("%s", s);
			int l = strlen(s);
			int k = maxn-l;
			for(int j=k; j<maxn; j++)
			{
				mp[j][s[j-k]-'a']++;
			}
			if(l!=1)
				jilu[s[0]-'a'] = 1;
		}
		for(int i=0; i<26; i++)
        {
            for(int j=maxn-1; j>0; j--)
            {
                if(mp[j][i]>=26)
                {
                    mp[j-1][i] += mp[j][i]/26;
                    mp[j][i] = mp[j][i]%26;
                }
            }
        }

		for(int i=0; i<26; i++)
		{
			for(int j=maxn-1; j>=0; j--)
			{
				node[i].num[j] += mp[j][i];
			}
		}

		sort(node, node+26, cmp);
		for(int i=25; i>=0; i--)
		{
			if(!jilu[node[i].x])
				{
				    zero = node[i].x;
				    break;
				}
		}
		for(int i=0; i<26; i++)
            {
            	if(node[i].x != zero)
            		vis[node[i].x] = h--;
            }

        for(int i=maxn-1; i>=0; i--)
		{
			for(int j=0; j<26; j++)
				mm[i] += mp[i][j]*vis[j];
		}
		ll jk = 1;
		sum = mm[maxn-1];
		for(int i=maxn-2; i>=0; i--)
		{
			jk = (jk * 26)%mod;
			sum = (sum + mm[i] * jk)%mod;
		}
		printf("Case #%d: %lld\n", kk, sum);
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值