Balala Power!(大数+思维)

Balala Power!

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

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. (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 个小写的字符串,想要把它们变为 26 进制的 0-25 的数字,问 n 个数最大的和为多少?还有,不能有前导0,除非就是0

题解:每个字母,在任意位置都会有个权值,统计所有字母的权值,但是只能用大数保存,保存完后。按字母权值排序,最大的赋 24 ,类推,然后考虑可能前导 0 的情况,如果,找到不会作为前导的数后,,关键是,不能交换,而是整体向上平移 1 ,这样才能保证是最大值

  1 #include <iostream>
  2 #include <string.h>
  3 #include <stdio.h>
  4 #include <algorithm>
  5 using namespace std;
  6 #define LL long long
  7 #define MOD 1000000007
  8 #define MX 100100
  9
 10 int n;
 11 int al_n[26];
 12 LL num[26][MX];
 13 LL quan[26];
 14 char temp[MX];
 15 bool ok[26];
 16
 17 bool cmp(int a,int b)
 18 {
 19     if (al_n[a]!=al_n[b])
 20         return al_n[a]>al_n[b];
 21
 22     for (int i=al_n[a];i>=0;i--)
 23         if (num[a][i]!=num[b][i])
 24         return num[a][i]>num[b][i];
 25
 26     return 0;
 27 }
 28
 29 int main()
 30 {
 31     int cnt=1;
 32     while (scanf("%d",&n)!=EOF)
 33     {
 34         memset(quan,0,sizeof(quan));
 35         memset(num,0,sizeof(num));
 36         memset(ok,0,sizeof(ok));
 37
 38         for (int i=0;i<n;i++)
 39         {
 40             scanf("%s",temp);
 41             int len = strlen(temp);
 42             LL k=1;
 43             for (int j=len-1;j>=0;j--)
 44             {
 45                 num[temp[j]-‘a‘][k]++;
 46                 k++;
 47             }
 48             if (len!=1)
 49                 ok[temp[0]-‘a‘]=1;
 50         }
 51
 52         for (int i=0;i<26;i++)//字母
 53         {
 54             al_n[i]=0;
 55             for (int j=1;j<MX;j++) //长度
 56             {
 57                 if (num[i][j]) al_n[i]=j;
 58                 if (num[i][j]>=26)
 59                 {
 60                     int jin = num[i][j]/26;
 61                     num[i][j+1]+=jin;
 62                     num[i][j]%=26;
 63                 }
 64             }
 65         }
 66
 67         int alpa[26];
 68         for (int i=0;i<26;i++) alpa[i]=i;
 69         sort(alpa,alpa+26,cmp);
 70
 71         if (al_n[alpa[25]]!=0&&ok[alpa[25]]==1)
 72         {
 73             for (int i=25;i>=0;i--)
 74             {
 75                 if (ok[alpa[i]]==0)
 76                 {
 77                     int sbsb=alpa[i];
 78                     for (int j=i+1;j<=25;j++)
 79                         alpa[j-1]=alpa[j];
 80                     alpa[25]=sbsb;
 81                     break;
 82                 }
 83             }
 84         }
 85
 86         LL ans = 0;
 87         LL qqq = 25;
 88         for (int i=0;i<26;i++)
 89         {
 90             int zimu = alpa[i];
 91             if (al_n[zimu]==0) continue;
 92             LL tp=qqq;
 93             for (int j=1;j<=al_n[zimu];j++)
 94             {
 95                 ans = (ans + (tp * num[zimu][j])%MOD)%MOD;
 96                 tp=(tp*26)%MOD;
 97             }
 98             qqq--;
 99         }
100         printf("Case #%d: %lld\n",cnt++,ans);
101     }
102     return 0;
103 }

时间: 2024-10-13 01:52:11

Balala Power!(大数+思维)的相关文章

HDU 6034 Balala Power!【排序/进制思维】

Balala Power![排序/进制思维] Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 6703    Accepted Submission(s): 1680 Problem Description Talented Mr.Tang has n strings consisting of only lower case cha

HDU 6034 Balala Power!(贪心+排序)

Balala Power! Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 1411    Accepted Submission(s): 239 Problem Description Talented Mr.Tang has n strings consisting of only lower case characters.

第一场 A Balala Power!

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 zinto each number ranged from 0 to 25, but each two different characters should not b

hdu 6034 Balala Power!

题目链接:hdu 6034 Balala Power! 题意: 给你n个字符串,都是包含小写字母,现在让你给a~z赋值0~25,使得这些字符串变成的26进制的数的总和最大. 不能有前导0的情况,他们保证至少有一个字母不出现在第一位. 题解: 每个字符对答案的贡献都可以看作一个 26 进制的数字,问题相当于要给这些贡献加一个 0 到 25 的权重使得答案最大.最大的数匹配 25,次大的数匹配 24,依次类推.排序后这样依次贪心即可,唯一注意的是不能出现前导 0. 前导0的具体处理看代码. 1 #i

2017 Multi-University Training Contest - Team 1 1002&amp;&amp;HDU 6034 Balala Power!【字符串,贪心+排序】

Balala Power! Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 2668    Accepted Submission(s): 562 Problem Description Sample Input 1 a 2 aa bb 3 a ba abc Sample Output Case #1: 25 Case #2: 132

Balala Power! HDU - 6034

Balala Power! HDU - 6034 题意:给n个字符串,让你给每一个小写字母赋一个值(0到25),使得所有的字符串的总和最大. 把每一个字符串看作一个26进制的数 先统计每个字母在不同位置出现的次数,然后按出现次数的多少排序,出现的多的自然赋大的值 注意题目要求不能有前导零,所以排序后拍到最后的那个字母如果在某个字符串首位出现过,就要和前面的换一下 1 #include <bits/stdc++.h> 2 using namespace std; 3 #define ll lon

Secret Project Gym - 101972(组合数大数+思维)

题目:有 n 个人,x把锁,(每个人有y把钥匙),要求最少有 m 个人在场才能打开这所有的x把锁,问 x y 的最小值分别是多少. 分析: 1.公式:https://blog.csdn.net/SunMoonVocano/article/details/84137160 2.组合数大数求解,因为涉及到模运算,所以需要用到乘法逆元:也即边打阶乘的表,边求阶乘关于mod的逆元. 代码: 1 #include <bits/stdc++.h> 2 using namespace std; 3 type

HDU 6034 Balala Power! (贪心+坑题)

题意:给定一个 n 个字符串,然后问你怎么给 a-z赋值0-25,使得给定的字符串看成26进制得到的和最大,并且不能出现前导0. 析:一个很恶心的题目,细节有点多,首先是思路,给定个字符一个权值,然后要注意的进位,然后排序,从大到小,给每个字符赋值,如果最后一个出现前导0,就得向前找一个最小的不在首字符的来交换,但不能动了相对的顺序. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <c

2017 Multi-University Training 1 解题报告

Add More Zero Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 2245    Accepted Submission(s): 1053 Problem Description There is a youngster known for amateur propositions concerning several ma