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


Problem Description
Balala Power!(大数+思维)

Talented Mr.Tang has 7.
 

 

Input
The input contains multiple test cases.

For each test case, the first line contains one positive integers )
 

 

Output
For each test case, output "Case #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 }
View Code

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-10-25
  • 2021-11-27
  • 2021-11-01
  • 2022-01-10
  • 2021-04-13
  • 2021-09-25
猜你喜欢
  • 2021-07-11
  • 2021-06-12
  • 2022-12-23
  • 2022-01-01
  • 2021-08-14
  • 2022-01-23
  • 2022-12-23
相关资源
相似解决方案