HDU 6034

Balala Power!

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


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

大意:给出的字符串,每个字符建立一种与0-25的对应关系。然后每个字符串看成是一个26进制的数。问能获得的数的总和的最大值。(最后对1e9+7取模)。

所有的字符串,无非就是每个字符的贡献乘上赋予的映射的权值。所以我们可以按照贡献排序。贡献其实就是跟位置有关的系数,我们把对应的位置记录下来,转换成一个26进制数。

官方题解:每个字符对答案的贡献都可以看作一个 26 进制的数字,问题相当于要给这些贡献加一个 0 到 25 的权重使得答案最大。最大的数匹配 25,次大的数匹配 24,依次类推。排序后这样依次贪心即可,唯一注意的是不能出现前导 0。

#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5+7;
const long long mod = 1e9+7;
int n;
struct node
{
    int id;
    int num[MAXN];
    bool operator < (const node &a)const
    {
        for(int j = 100000; j >= 0; --j)if(num[j] != a.num[j])return num[j] > a.num[j];
        return 0;
    }
}p[30];
string s[MAXN];
long long num[30];
long long k[MAXN];
bool ha[30];
int main()
{
    ios::sync_with_stdio(false);
    k[0] = 1;
    for(int i = 1 ; i <= 100000; ++i)k[i] = (k[i-1]*26)%mod;

    int ca = 0;
    while(cin>>n)
    {
        for(int i = 0; i < 26; ++i)
        {
            for(int j=0; j<=100000; j++)p[i].num[j]=0;
            p[i].id = i;
            ha[i] = 0;
        }

        for(int i = 0; i < n; ++i)
        {
            cin>>s[i];
            int l = s[i].size();
            if(l > 1)ha[s[i][0]-'a'] = 1;
            for(int ii = l-1,j=0; ii >=0; --ii,++j)
            {
                int t = s[i][ii]-'a';
                p[t].num[j]++;
            }
        }

        for(int i = 0; i < 26; ++i)
        {
            for(int j = 0; j <= 100000; ++j)
            {
                p[i].num[j+1] += p[i].num[j]/26;
                p[i].num[j]%=26;
            }
        }
        sort(p,p+26);
        for(int i = 25; i >= 0; --i)num[p[25-i].id] = i;
        int t = 25;
        while(ha[p[t].id]&&t)
        {
            swap(num[p[t].id],num[p[t-1].id]);
            t--;
        }
        long long ans = 0;
        for(int i = 0; i < n; ++i)
        {
            int l = s[i].size();
            for(int j = 0; j < l; ++j)
            {
                ans = (ans+num[s[i][j] -'a']*k[l-1-j]%mod)%mod;
            }
        }
        cout<<"Case #"<<++ca<<": "<<ans<<endl;
    }
    return 0;
}






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值