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. (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
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
",cnt++,ans);
101     }
102     return 0;
103 }
View Code
原文地址:https://www.cnblogs.com/haoabcd2010/p/7236214.html