hdu 6034 B - Balala Power! 贪心

B - Balala Power!

题目连接

http://acm.hdu.edu.cn/showproblem.php?pid=6034php

题面描述

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.node

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.c++

The summation may be quite large, so you should output it in modulo 109+7.app

输入

The input contains multiple test cases.ui

For each test case, the first line contains one positive integers n, the number of strings. (1≤n≤100000)this

Each of the next n lines contains a string si consisting of only lower case letters. (1≤|si|≤100000,∑|si|≤106)spa

输出

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.code

样例输入

1
a
2
aa
bb
3
a
ba
abcip

样例输出

Case #1: 25
Case #2: 1323
Case #3: 18221ci

题意

给你n个由26个字母组成的字符串,你如今要给每一个字母用[0,25]赋值,不能要求有前导0,每一个值对应一个字母.

要求使得字符串组成的数字和最大。

题解

直接算每一个字母的贡献,而后赋值就行,把0给最小的合法的字母便可。

代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+7;
const int mod = 1e9+7;
struct node{
    int len[maxn];
    int w;
    int v;
    int L;
}p[26];
int n;
string s[maxn];
int flag[26];
int cas = 0;
bool cmp(node A,node B){
    if(A.L!=B.L)return A.L<B.L;
    for(int i=A.L;i>=0;i--){
        if(A.len[i]==B.len[i])continue;
        return A.len[i]<B.len[i];
    }
    return 0;
}
int main(){
    while(cin>>n){
        cas++;
        memset(flag,0,sizeof(flag));
        for(int i=0;i<26;i++){
            for(int j=0;j<=p[i].L;j++){
                p[i].len[j]=0;
            }
        }
        for(int i=0;i<26;i++){
            p[i].v=0;
            p[i].w=i;
            p[i].L=-1;
        }
        for(int i=0;i<n;i++){
            cin>>s[i];
            if(s[i].size()>1){
                flag[s[i][0]-'a']++;
            }
            reverse(s[i].begin(),s[i].end());
            for(int j=0;j<s[i].size();j++){
                p[s[i][j]-'a'].len[j]++;
            }
        }
        for(int i=0;i<26;i++){
            for(int j=0;j<maxn-1;j++){
                if(p[i].len[j]>=26){
                    int d = p[i].len[j]/26;
                    p[i].len[j]%=26;
                    p[i].len[j+1]+=d;
                }
                if(p[i].len[j]>0){
                    p[i].L=max(p[i].L,j);
                }
            }
        }
        sort(p,p+26,cmp);
        for(int i=0;i<26;i++){
            p[i].v=i;
        }
        for(int i=0;i<26;i++){
            if(flag[p[i].w]&&p[i].v==0){
                swap(p[i].v,p[i+1].v);
            }else break;
        }
        long long ans = 0;
        long long value[26];
        for(int i=0;i<26;i++){
            value[p[i].w]=p[i].v;
        }
        for(int i=0;i<n;i++){
            long long now = 1;
            for(int j=0;j<s[i].size();j++){
                ans=(ans+(value[s[i][j]-'a']*now)%mod)%mod;
                now=(now*26)%mod;
            }
        }
        cout<<"Case #"<<cas<<": "<<ans<<endl;
    }
}
相关文章
相关标签/搜索