HDU 6034 Balala Power!(大数进制)

7 篇文章 0 订阅

Balala Power!

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


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
 

Source
 

   

题意:

a-z分别可变为0-25,给你n个字符串,把变成26进制的数,加起来合最大。每个字符串除了单个0,没有前置0。


point:

算出各个字母的价值,开一个char数组(int数组会报错!耽误了好久)来存下字母在第几位上出现了几次,因为字符串很长,所以要开char[100007]。并且由于26进制,在某位上超过了26就进1清零,这个和大数运算相同。

在根据每个char数组代表的价值来进行排序。注意有些字母是不能为0的。

以上在比赛中都实现了,由于一些小细节方面就一直WA。

所以要先确定哪个是0,当然是从价值最小的开始找,找到则标记。

然后在从价值最大的开始找,分别赋值从25到1,但要注意0位置的上下。

可以先打个 26^1-100000的表。计算起来比较方便。


#include <stdio.h>
#include <string.h>
#include <iostream>
#include <map>
#include <algorithm>
#include <math.h>
using namespace std;
#define rt x<<1|1
#define lt x<<1
#define  LL long long
const LL p = 1e9+7;
LL m[100000+5];
const int maxn=100000+7;
int vis[26];
struct node
{
    int flag;
    char num[maxn];
}num[27];
void init()
{
    m[0]=1;
    LL now=1;
    for(int i=1;i<=100000;i++)
    {
        now*=26;
        m[i]=now%p;
        now=now%p;
    }
}
bool cmd(node a,node b)
{
    for(int i=maxn-1;i>0;i--)
    {
        if(a.num[i]!=b.num[i])
        {
            return a.num[i]>b.num[i];
        }
        else ;
    }
    return a.num[0]>b.num[0];
}
int main()
{
    int n;
    init();
    int pp=0;
    while(~scanf("%d",&n))
    {
        char str[maxn];
        for(int i=0;i<26;i++)
        {
            num[i].flag=i;
            vis[i]=0;
            for(int j=0;j<maxn;j++) num[i].num[j]=0;
        }
        for(int i=1;i<=n;i++)
        {
            scanf("%s",str);
            int l=strlen(str);
            if(l!=1) vis[str[0]-'a']=1;
            for(int j=0;j<l;j++)
            {
                int x=str[j]-'a';
                int y=l-j-1;
                num[x].num[y]++;
                while(num[x].num[y]==26)//防止一直进位,用while-这里很重要,不仔细就wa了
                {
                    num[x].num[y++]=0;
                    num[x].num[y]++;
                }
            }
        }
        sort(num,num+26,cmd);
        LL ans = 0;
        int f=-1;
        for(int i=25;i>=0;i--)
        {
            if(!vis[num[i].flag])
            {
                f=i;
                break;
            }
        }
        for(int i=0;i<=25;i++)
        {
            if(i==f);
            else if(i<f)
            {
                LL haha=25-i;
                for(int j=maxn;j>=0;j--)
                {
                    (ans+=(LL)haha*m[j]*num[i].num[j])%=p;
                }
            }
            else
            {
                LL haha=25-i+1;
                for(int j=maxn;j>=0;j--)
                {
                    (ans+=(LL)haha*m[j]*num[i].num[j])%=p;
                }
            }
            
        }
        
        printf("Case #%d: %lld\n",++pp,ans);
        
    }
    
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值