【多校训练】hdu 6034 Balala Power!

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个字符串,给26个字母赋值0-25,使字符串的和最大,不能有前导0,求最大值。

思路:

可以统计下每个字母的权重,权重最大的字母赋值为25,,以此类推。要让非首字母且权重相对较小的字母为0。

//
//  main.cpp
//  1002
//
//  Created by zc on 2017/7/25.
//  Copyright © 2017年 zc. All rights reserved.
//

#include <iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#define ll long long
using namespace std;
const ll MOD=1e9+7;
char s[120000];
int a[27][120000];
int b[27],c[27],e[27];
ll d[120000];
bool cmp(int x,int y)
{
    for(int i=110000;i>=0;i--)
    {
        if(a[x][i]!=a[y][i])    return a[x][i]<a[y][i];
    }
    return 0;
}

int main(int argc, const char * argv[]) {
    int n,kase=0;
    d[1]=1;
    for(int i=2;i<=110000;i++)  d[i]=(d[i-1]*26)%MOD;
    while(~scanf("%d",&n))
    {
        memset(a,0,sizeof(a));
        memset(e,0,sizeof(e));
        for(int i=0;i<n;i++)
        {
            scanf("%s",s);
            int len=strlen(s);
            for(int j=len-1;j>=0;j--)
            {
                a[s[j]-'a'][len-j]++;
                if(j==0)    e[s[j]-'a']=1;
            }
        }
        for(int i=0;i<26;i++)
        {
            for(int j=1;j<=110000;j++)
            {
                if(a[i][j]>26)
                {
                    a[i][j+1]+=a[i][j]/26;
                    a[i][j]%=26;
                }
            }
        }
        for(int i=0;i<26;i++)   b[i]=i;
        sort(b,b+26,cmp);
        if(e[b[0]]==1)
        {
            int t=1;
            while(t<26&&e[b[t]]==1) t++;
            int tt=b[t];
            for(int i=t-1;i>=0;i--)    b[i+1]=b[i];
            b[0]=tt;
        }
        for(int i=0;i<26;i++)   c[b[i]]=i;
        ll ans=0;
        for(int i=0;i<=26;i++)
        {
            for(int j=1;j<=110000;j++)
            {
                ans=(ans+c[i]*a[i][j]*d[j])%MOD;
            }
        }
        printf("Case #%d: %lld\n",++kase,ans);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值