HDU 6034 Balala Power!

来源:互联网 发布:kettle java代码 编辑:程序博客网 时间:2024/05/24 01:42

Balala Power!

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


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 #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
1a2aabb3abaabc
 

Sample Output
Case #1: 25Case #2: 1323Case #3: 18221
 

Source
2017 Multi-University Training Contest - Team 1

#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <algorithm>#include <cmath>using namespace std;typedef long long LL;const int INF = 0x3f3f3f3f;const int maxn = 2e5+10;const LL MOD = 1e9+7;int N,vist[30],level[30];int Map[30][maxn];int Max;char str[maxn];struct node{    char st[maxn];    int id;}nxt[30];bool cmp(node a,node b){    return strcmp(a.st,b.st)>0;}int main(){    int cas = 0;    while(~scanf("%d",&N)){        memset(Map,0,sizeof(Map));        memset(vist,0,sizeof(vist));        Max = 0;        while(N--){            scanf("%s",str);            int len = strlen(str);            Max = max(Max,len);            for(int i = 0; i < len; i++)                Map[str[i]-'a'][len-i-1]++;            if(len > 1)                vist[str[0]-'a'] = 1;        }        Max += 10000;//继续向上进位        for(int i = 0; i < 26; i++){            for(int j = 0; j < Max; j++){                Map[i][j+1] += Map[i][j]/26;                Map[i][j] %= 26;                nxt[i].st[Max-j-1] = Map[i][j]+'a';            }            nxt[i].id = i;        }        sort(nxt,nxt+26,cmp);        for(int i = 0; i < 26; i++){            level[nxt[i].id] = 25-i;        }        int t = 25;        while(vist[nxt[t].id] && t){//获取替换是最小代价            swap(level[nxt[t].id],level[nxt[t-1].id]);            t--;        }        LL ans,res;        ans = 0;        for(int i = 0; i < 26; i++){            res = 0;            for(int j = 0; j < Max; j++){                res = res*26+(nxt[i].st[j]-'a')*level[nxt[i].id];                res %= MOD;            }            ans += res;            ans %= MOD;        }        printf("Case #%d: %lld\n",++cas,ans);    }    return 0;}


原创粉丝点击