HDU 6034 Balala Power! 【模拟】【大数高精度】

这篇博客主要介绍了如何解决HDU 6034题目的过程,涉及模拟和大数高精度计算。作者分享了题目链接、题目描述,并阐述了解题思路,最后给出了自己的代码实现。

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

Balala Power!

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


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

Source
 

Recommend
liuyiding


题目链接:

题目大意:

n个字符串,将a-z挑出对应的字母,赋值为0-25,假设赋值后每位字母都变成了qq[对应字母]这样对应第n位上的字母就成了qq[对应字母]*26^n。
要求不出现前导零的情况下,这个数字的最大值为多少。

解题思路:

①.抛去不允许前导零的条件,这就是一个贪心的问题,哪个字母占的权值之和最大,就给它赋最大的值。最初想找个数组存每个字母占的权值之和,然后比较后赋值,这样就可以直接算出答案来了,但是问题是怎么记录每个字母的权值之和呢?26^100000,根本存不下来,于是换个思路。因为大数加法就是用数组模拟,而这个类似,那我可以把每个字母当作一个数值,然后按大数加法来看,用num[26][100005]的数组来记录对应每一位权值上这个字母出现的次数,遇到超过26次的就进位,然后把权值从大到小来比较就好了。
②.因为不允许出现前导零,所以即使它所占的权值比重最小,也不能让它为0。这样,在读取字符串时我们顺便标记一下哪个字母不能赋值为0,计算最后结果的时候,如果遇到最小权值的字母为0的情况,就把后面可以作为0的往前换。

Mycode:

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int MAX = 100000+5;
const int MOD = 1e9+7;
const int INF = 0x3f3f3f3f;

LL ans;
int t, lens, len;
bool ban[26]; //不可为0的字母,1表示不可为0
char str[MAX];
int num[26][MAX];
LL ppow[MAX] = {1};

int main()
{
    int cas = 0;
    //预处理26^n
    for(int i = 1; i < MAX-3; ++i)
        ppow[i] = ppow[i-1] * 26 % MOD;

    while(~scanf("%d",&t))
    {
        ans = 0;
        lens = 0; //最大字符串的长度
        memset(num, 0, sizeof(num));
        memset(ban, 0, sizeof(ban));

        //标记对应字母
        for(int i = 0; i < 26; ++i)
            num[i][MAX-3] = i;

        while(t--)
        {
            scanf("%s", str);
            len = strlen(str);
            if(lens < len) lens = len;

            bool f = false;
            if(len > 1)
            {
                for(int i = 1; i < len; ++i)
                {
                    if(str[i] != str[0])
                    {
                        f = true;
                        break;
                    }
                }
            }
            if(f) ban[str[0]-'a'] = true;

            reverse(str, str + len);
            for(int i = 0; i < len; ++i)
                ++num[str[i]-'a'][i];
        }

        for(int i = 0; i < 26; ++i)
            for(int j = 0; j <= lens; ++j)
            {
                if(num[i][j] >= 26)
                {
                    num[i][j+1] += num[i][j] / 26;
                    num[i][j] %= 26;
                }
            }

        //排序
        for(int i = 0; i < 25; ++i)
        {
            for(int j = 0, k = lens+1; j < 25; ++j, k = lens+1)
            {
                while(num[j][k] == num[j+1][k] && k != 0)
                    --k;
                if(num[j][k] < num[j+1][k])
                    swap(num[j],num[j+1]);
            }
        }

        //“去0”
        for(int i = 25; i >= 0; )
        {
            if(ban[num[i][MAX-3]])
                --i;
            else
            {
                for(int j = i; j < 25; ++j)
                    swap(num[j], num[j + 1]);
                break;
            }
        }

        for(int i = 0; i < 26; ++i)
        {
            for(int j = 0; j <= lens+1; ++j)
            {
                ans += (ppow[j] * num[i][j] % MOD * (25 - i) % MOD);
                ans %= MOD;
            }
        }

        printf("Case #%d: %lld\n", ++cas, ans);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值