HDU 6034 Balala Power!(大數進位制)

Mr_Treeeee發表於2020-04-06

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 #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
 

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);
        
    }
    
}

相關文章