hdu6034

Balala Power!

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


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.  (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 # 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
思路:在多校联赛上一直没有做出来,后面清题时才发现如果直接存权值的话long long int根本不够用,如果把权值直接取模的话,那么就会影响后面的赋值,因此需要开一个二维数组来记录权值,a[i][j]代表第i个字母在j位置出现可了多少次,这是26进制的,所以满26向前面进一位,一开始用结构体做,结果TLE了,后面发现用多个数组进行排序其实更快。其实,一个数组可以通过另一个数组来排序。
代码:
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <cmath>
const long long int MOD=1e9+7;
using namespace std;
long long int poww(int n,int m)
{
    long long int a=1;
    while(m)
    {
        if(m&1)a=(a*n)%MOD;
        n=(n*n)%MOD;
        m>>=1;
    }
}
int a[27][100020];
int flag[27];
int v[27];
int ss[27];
bool cmp(int x,int y)
{
    int i;
     for(i=100019;i>=0;i--)
     {
         if(a[x][i]>a[y][i])return true;
         if(a[x][i]<a[y][i])return false;
     }
     return false;
}
int main()
{
    int m;
    int sss=1;
    while(scanf("%d",&m)!=EOF)
    {
        int i;
        memset(a,0,sizeof(a));
        memset(flag,0,sizeof(flag));
        memset(ss,0,sizeof(ss));
        memset(v,-1,sizeof(v));
        char c[100005];
        for(i=0;i<m;i++)
        {
            scanf("%s",c);
            int n=strlen(c);
            if(n>1)flag[c[0]-'a'+1]=1;
            for(int j=0;j<n;j++)
            {
               a[c[j]-'a'+1][n-j-1]++;
            }
        }
        for(i=1;i<=26;i++)
        {
            for(int j=0;j<100020;j++)
            {
                if(a[i][j]>=26)
                {
                    a[i][j+1]+=a[i][j]/26;
                    a[i][j]%=26;

                }
            }
        }
        int s[27];
        for(i=1;i<=26;i++)
        {
            s[i]=i;
        }
        sort(s+1,s+1+26,cmp);
        int k=25;
        for(i=26;i>=1;i--)
        {
            if(flag[s[i]]!=1)
            {
                v[s[i]]=0;
                break;
            }
        }
        for(i=1;i<=26;i++)
        {
            int x=s[i];
            if(v[x]==-1)
            {
                v[x]=k--;
            }
        }
        long long int ans=0;
        long long int e=1;
        for(i=0;i<100020;i++)
        {
            for(int j=1;j<=26;j++)
            {
                ans=(ans+((e*a[j][i])%MOD*v[j])%MOD)%MOD;
            }
            e*=26;
            e%=MOD;
        }
       printf("Case #%d: %lld\n",sss++,ans);
    }
    return 0;
}


 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值