Balala Power!
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 3757 Accepted Submission(s): 907
Problem Description
![](https://i-blog.csdnimg.cn/blog_migrate/206c742d60bb80a1bf080aa3f1c098a0.jpeg)
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. (1≤n≤100000)
Each of the next n lines contains a string si consisting of only lower case letters. (1≤|si|≤100000,∑|si|≤106)
For each test case, the first line contains one positive integers n, the number of strings. (1≤n≤100000)
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
Source
Recommend
/* * @Author: Lyucheng * @Date: 2017-07-26 11:03:09 * @Last Modified by: Lyucheng * @Last Modified time: 2017-07-26 17:24:29 */ /* 题意:给你n个字符串,只包含a~z的字符,你可以给字符赋值0~25,但是不同的字符的值不能相同,要求得到n个字符串的和最大 思路:相当于n个26进制的数,然后考虑每个字符在每个字符串中位置对结果的贡献,贡献最多的为25,其次是24,以此类推,要 注意的问题就是前导零的问题 */ #include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> #include <vector> #include <queue> #include <set> #include <map> #include <string> #include <math.h> #include <stdlib.h> #include <time.h> #define LL long long #define MAXN 100005 #define MAXK 26 const LL MOD=1e9+7; using namespace std; struct Node{ int pos;//标记字符 int num[MAXN]; bool operator < (const Node & other) const {//重载小于号按照优先级排序 for(int i=MAXN-1;i>0;i--){ if(num[i]!=other.num[i]) return num[i]<other.num[i]; } return num[0]<other.num[0]; } }node[MAXK+1]; int n; char str[MAXN]; bool vis[MAXK];//标记是否不能为第一个字符 int len; int ca=1; inline void init(){ for(int i=0;i<MAXK;i++){ node[i].pos=i; for(int j=0;j<MAXN;j++){ node[i].num[j]=0; } } memset(vis,false,sizeof vis); } int main(){ // freopen("in.txt", "r", stdin); // freopen("out.txt", "w", stdout); while(scanf("%d",&n)!=EOF){ init(); for(int i=0;i<n;i++){ scanf("%s",str); len=strlen(str); if(len>1){//如果不是单个字符那么,第一个字符就不能为零 vis[str[0]-'a']=true; } for(int j=0;j<len;j++){ int x=str[j]-'a';//字符 int y=len-j-1;//位置 node[x].num[y]++; while(node[x].num[y]==MAXK){//满了26个了,不管是几都要进位 node[x].num[y++]=0; node[x].num[y]++; } } } sort(node,node+MAXK); //找第一个能为零的字符 int pos=-1; for(int i=0;i<MAXK;i++){//从优先级最小的开始找 if(vis[node[i].pos]==false){ pos=i; break; } } int POW=0; LL res=0; for(int i=0;i<MAXK;i++){ if(i==pos){ POW=0; }else if(i<pos){ POW=i+1; }else{ POW=i; } LL cur=0; for(int j=MAXN-1;j>=0;j--){ cur = (cur * MAXK) % MOD; cur = (cur + (LL)node[i].num[j] * POW) % MOD; } res = (res + cur)%MOD; } printf("Case #%d: %lld\n",ca++,res); } return 0; }