Balala Power! (模拟)

xiaoxiao2021-02-28  138

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. (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 题目大意:输入一个n代表有n个字符串,每个字符串都用小写字母表示,现可用0—25这26个数字分别来表示25个字母,使得字符串表示的结果最大。 解题思路:用一个结构体数组记录着每个字母在每位上出现的次数(注意这里涉及着进位,如果一个字母在某一位出现的次数大于等于26,则需要将这个字母向它的高位进一位),对于统计好的字母从最高位开始每次找出现最多的数,分别从25—0对其赋值。这里需要注意的是题目中要求不能 出现前导0,所以我们要记录长不为1的第一个出现的字母,在对所有字母赋值后,检查出是否有前导0出现,若有则需要将前导0这个字母赋值为x,x为不出现在第一个的字母且从1,2,3··· ···一次递增的一个数。

#include <bits/stdc++.h> #define mod 1000000007 #define maxn 100005 using namespace std; struct Node { int id, pl[maxn]; bool operator < (const Node &a)const { for(int i = maxn-1;i>=0;i--) if(pl[i] > a.pl[i]) return 1; else if(pl[i] < a.pl[i]) return 0; else; } }; long long int f[maxn] = {1}; char str[maxn]; int vis[28],ch[28],Max_len; Node dp[28]; int main() { for(int i=1;i<100005;i++)///打表存下所有26的次方数 f[i] = (f[i-1]*26)%mod; int n,c=0; while(~scanf("%d",&n)){ Max_len = 0; memset(vis,0,sizeof(vis)); memset(ch,-1,sizeof(ch)); memset(dp,0,sizeof(dp)); int len; for(int i=0;i<n;i++){ scanf(" %s",str); len = strlen(str); Max_len = max(Max_len,len);///记录最高位 if(len != 1) vis[str[0] - 'a'] = 1;///记录哪些字母不能为前导0 for(int j=0;j<len;j++) dp[str[j]-'a'].pl[len-j-1]++;///记录每个字母在每一位上出现的次数 } for(int i=0;i<26;i++){ for(int j=0;j<maxn;j++) if(dp[i].pl[j]>=26){///如果存在大于等于26的字母,需要进位 dp[i].pl[j+1] += dp[i].pl[j]/26; dp[i].pl[j] %= 26; } dp[i].id = i; }//cout<<"a"<<endl; sort(dp,dp+26); for(int i = 0; i < 26; i++) ch[dp[i].id] = 26-i-1; for(int i = 0; i < 26; i++) if(vis[dp[i].id]&&ch[dp[i].id]==0){ for(int j = 25;j >= 0; j--){ if(!vis[dp[j].id]){ for(int k = 25; k >= j + 1; k--) ch[dp[k].id] = ch[dp[k-1].id]; ch[dp[j].id] = 0; break; } } break; } long long int ans = 0; for(int i=0;i<26;i++) { for(int j = 0; j < maxn; j ++) ans = (ans+f[j]*dp[i].pl[j]*ch[dp[i].id]%mod)%mod; } printf("Case #%d: %lld\n", ++c, ans); } return 0; }
转载请注明原文地址: https://www.6miu.com/read-44373.html

最新回复(0)