HDU 6034 Balala Power! —— Multi-University Training 1

本文介绍了一个关于26进制数转换的问题,通过将字母映射到26进制数中,实现字符串到数值的转换,并求得一系列字符串转换后的数值之和的最大值。

  Talented Mr.Tang has nn strings consisting of only lower case characters. He wants to charge them with Balala Power (he could change each character ranged from a to 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 2626 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+7109+7.

Input

  The input contains multiple test cases. 

Output

  For each test case, the first line contains one positive integers nn, the number of strings. (1n100000)

  Each of the next nn lines contains a string sisi consisting of only lower case letters. (1|si|100000,|si|1e6)e
OutputFor each test case, output " Case #xx: yy" in one line (without quotes), where xxindicates the case number starting from 11 and yy 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

题目大意:用26个字母表示26进制数中的1~26,并且给你n个字符串,以26进制从字符串转化为数字,问这些数字之和最大能有多少(以十进制输出)。为保证数据正确,其中必存在至少一个字母是代表了0。

思路:模拟。首先每个字母代表的数未知,但由于每个字符在字符串的位置是已知的,我们就可以求出每个字母从26进制转化十进制时的“系数”(即像26^a1+26^a2+26^a3……的数),用数组存储26进制数,在求和时考虑进位,去掉必为0的字母,然后对按26进制数的大小对数组排序,最后就能够求和解出答案了。。。比赛的时候是用的字符串存26进制,理论上也是可以做的,但是因为进位的姿势不对(一边加一边进位,先全部加完再进位才是正确的)导致TLE/(ㄒoㄒ)/

 

代码

 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 #include<algorithm>
 5 #include<iomanip> 
 6 #include<cmath>
 7 #define MOD 1000000007  
 8 #define MAXN 100010  
 9 using namespace std;
10 long long num[30][MAXN],w[26];
11 long long sum[30];
12 long long a[MAXN];
13 int u;    
14 char ss[MAXN];
15 bool vis[1010];
16 bool cmp(int s, int t)
17 {
18     for(int i=u-1;i>=0;i--){
19         if(num[s][i]!=num[t][i])
20             return num[s][i]<num[t][i];
21     }
22     return 0;
23 }
24 int main()
25 {
26     a[0]=1;
27     for(int i=1;i<MAXN-1;i++)
28         a[i]=26*a[i-1]%MOD;
29         
30     int n;
31     int z=0;
32     while(~scanf("%d", &n))
33     {       
34         u=0;
35         memset(num, 0, sizeof(num));
36         memset(sum, 0, sizeof(sum)); 
37         memset(vis,false,sizeof(vis));
38         int wei,p;
39         for(int i=0;i<n;i++){
40             scanf("%s", ss);
41             int l=strlen(ss);
42             if(l>1)
43                 vis[ss[0]-'a']=true;
44             
45             for(int j=0;j<l;j++){
46                 p=ss[j]-'a';
47                 wei=l-1-j;
48                 num[p][l-1-j]++;
49                 sum[p]+=a[wei];
50                 sum[p]%=MOD;
51             }
52             u=max(u, l);
53         }
54         for(int i=0;i<26;i++){
55             for(int j=0;j<u;j++){ 
56                 num[i][j+1]=num[i][j+1]+num[i][j]/26;
57                 num[i][j]=num[i][j]%26;    
58             }
59             while(num[i][u])
60             {
61                 num[i][u+1]+=num[i][u]/26;
62                 num[i][u++]%=26;
63                 u++;
64             }
65             w[i]=i; 
66         }
67         int cnt=-1;
68         sort(w, w+26, cmp);
69         for(int i=0;i<26;i++){
70             if(!vis[w[i]]){
71                    cnt=w[i];
72                    break;
73             }
74         }
75         //cout<<cnt<<endl;
76         int res=0,x=25;
77         for(int i=25;i>=0;i--){
78             if(cnt!=w[i]){
79                 res+=(long long)(x--)*sum[w[i]]%MOD;
80                 res%=MOD;
81             }
82                 
83         }
84         z=z+1;
85         printf("Case #%d: %lld\n",z,res);
86     }
87 } 

 

转载于:https://www.cnblogs.com/MasterSpark/p/7265712.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值