2017杭電多校聯賽-Balala Power!

Balala Power!

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


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 froma 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 #xy" 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進制數,
所以我們可以先將這些26進制數轉換爲10進制,然後相加求和。具體看代碼。
ac代碼:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define ll long long 
const int N=105005;
const int mod=1000000007;
ll p[N];
int f[26],q[26],cnt[26][N]; 
char s[N];
int mysort(){
    int i,j,k;
    for(i=0;i<26;i++){
        for(j=i+1;j<26;j++){
            for(k=N-1;k>=0;k--){
                if(cnt[q[i]][k]<cnt[q[j]][k]){
                    int t=q[i];
                    q[i]=q[j];
                    q[j]=t;
                    break;
                }
                if(cnt[q[i]][k]>cnt[q[j]][k])
                    break;
            } 
        } 
    }
    if(f[q[25]]==1)
    {
        for(i=24;i>=0;i--){
            if(!f[q[i]]){
                for(j=i;j<25;j++)
                    q[j]=q[j+1];
                break;
            }
        }
    }
    
}
void init(){
    p[0]=1;
    for(int i=1;i<N;i++)
        p[i]=p[i-1]*26%mod;
}
int main(){
    ll ans;
    int i,j,n,m,x,t=1;
    init(); 
    while(~scanf("%d",&n)){ 
        memset(cnt,0,sizeof(cnt));
        memset(f,0,sizeof(f));
        for(i=0;i<26;i++)
            q[i]=i;
        for(i=0;i<n;i++){
            scanf("%s",s);
            m=strlen(s);
            for(j=0;j<m;j++){
                cnt[s[j]-'a'][m-j-1]++;
            }
            f[s[0]-'a']=1;
        }
        for(i=0;i<26;i++){
            for(j=0;j<N-1;j++){
                cnt[i][j+1]+=cnt[i][j]/26;
                cnt[i][j]%=26;    
            }
        }
        mysort();
        for(ans=i=0;i<25;i++){
            for(j=N-1;j>=0;j--)
            {
                if(cnt[q[i]][j]){
                    ans+=p[j]*cnt[q[i]][j]*(25-i),ans%=mod; 
                }
            }
        }
        printf("Case #%d: %lld\n",t++,ans);
    } 
    return 0;
}

題目鏈接:點擊打開鏈接http://acm.hdu.edu.cn/contests/contest_showproblem.php?cid=759&pid=1002

發佈了83 篇原創文章 · 獲贊 63 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章