HDU6034 Balala Power!(思路+大数)

Balala Power!

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


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
 

    题意:给出 n个字符串,字符包括 a~z,分别给它们赋值 0~25,使得这些串表达成26进制时的和最大
求出每个位置每个字母的个数,倒着存,此时就有大数的思想,相当于一个26进制的大数,首先要对获得的大数进位,满26进1
然后比较每个字母的贡献,重载<,用sort对获得的大数排序,依次赋值
最后考虑前导0的情况,找到第一个最小的不能为前导0的位置,把该位置变为0,循环左移即可
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<string>
#include<algorithm>
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long ll;
const int N=100100;
const int MOD=1e9+7;
const int M=30;
struct node
{
    int num[N];
    int id;
    bool operator < (const node&a)const
    {
        for(int i=100000;i>=0;i--)
        {
            if(num[i]!=a.num[i])
                return num[i]>a.num[i];
        }
        return 0;
    }
}e[M];
bool vis[M];
ll num[M],re[N];
string s[N];
int main()
{
    ios::sync_with_stdio(false);
    re[0]=1;
    for(int i=1;i<N;i++)
    {
        re[i]=re[i-1]*26;
        re[i]%=MOD;
    }
    int n,q=0;
    while(cin>>n)
    {
        for(int i=0;i<26;i++)
        {
            for(int j=0;j<=100000;j++)
                e[i].num[j]=0;
            e[i].id=i;
            vis[i]=0;
        }

        for(int i=0;i<n;i++)
        {
            cin>>s[i];
            int lenn=s[i].length();
            for(int j=0,k=lenn-1;j<lenn;j++,k--)
            {
                int t=s[i][k]-'a';
                e[t].num[j]++;
            }
            if(lenn>1)vis[s[i][0]-'a']=1;
        }
        for(int i=0;i<26;i++)
        {
            for(int j=0;j<=100000;j++)
            {
                e[i].num[j+1]+=e[i].num[j]/26;
                e[i].num[j]%=26;
            }
        }
            sort(e,e+26);
            for(int i=25;i>=0;i--)
            {
               num[e[25-i].id]=i;
            }
            int t=25;
            while(vis[e[t].id]&&t)
            {
                swap(num[e[t].id],num[e[t-1].id]);
                t--;
            }
            ll ans=0;
           for(int i=0;i<n;i++)
           {
                  int len=s[i].length();
               for(int j=0;j<len;j++)
               {
                   ans=(ans+num[s[i][j]-'a']*re[len-1-j]%MOD)%MOD;
               }
           }
           cout<<"Case #"<<++q<<": "<<ans<<endl;

    }
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值