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;
    }
}
相關文章
相關標籤/搜索