HDU6034-Balala Power-模拟

Balala Power!

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

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. (1≤n≤100000)

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

2017 Multi-University Training Contest - Team 1

  题目意思就是给你多个字符串,然后找出一个映射,这各映射是将每个字母映射到0-25的满射,于是每个字符串就变成了一个26进制的数,问在不出现前导0的前提下使各数和最大的映射所对应的和。
  注意,虽然不能出现前导0,但是单个的0是可以出现的。

  就是一个模拟,思想难度也不大,就是代码写起来略有些麻烦
  于是我在看完标程代码以后把它默写了出来Orz
  思路就是先预处理出每一位对应的26进制权值,第一位是1,第二位是26,第三位是26*26这样子。依次读入字符串并处理,处理时先判断长度是否大于1,若是,把第一位字符标记为不能为0,对于每一位字符来说,对应位的权值加1,对应字符的总和(这里总和单独列出来的原因是总和很可能超过int/LL而且除了排序用外没有意义,但是排序可以用权值数组完成,所以在计算的时候直接把求模过程完成)加上对应位的权值(预处理出来的)
  然后首先把每个字符的权值数组处理成标准26进制形式,用该数组作为辅助对字符进行排序。从权值最小字符开始遍历到第一个可以为0的字符设置为0,再从最大的字符开始依次累加25*权值总和、24*权值总和、…、1*权值总和。
  

AC代码

#include <cstdio>
#include <iomanip>
#include <cstring>
#include <iostream>
#include <stack>
#include <vector>
#include <queue>
#include <cctype>
#include <cmath>
#include <algorithm>
#include <set>
#include <bitset>
#include <map>
#define INF 0x3f3f3f3f
#define ULL unsigned long long
#define LL long long
#define N 100100
#define eps 10e-9
#define mem(a,n) memset(a,n,sizeof(a))
#define fread freopen("in.txt","r",stdin)
#define fwrite freopen("out.txt","w",stdout)
using namespace std;
const int MOD=1e9+7;
int num[26][N];
char str[N];
bool head[26];
int pwr[N],sum[N],weight[26];
int L;

bool cmp(int idx1,int idx2){
    for(int i=L-1;i>=0;--i){
        if(num[idx1][i]!=num[idx2][i]){
            return num[idx1][i]<num[idx2][i];
        }
    }
    return false;
}

void work(int n)
{
    mem(num,0);
    mem(sum,0);
    mem(head,0);
    static int kase=0;
    int len;
    while(n--){
        cin>>str;
        len=strlen(str);
        L=max(L,len);
        if(len>1){
            head[str[0]-'a']=true;
        }
        reverse(str,str+len);
        for(int i=0;i<len;++i){
            ++num[str[i]-'a'][i];
            sum[str[i]-'a']+=pwr[i];
            if(sum[str[i]-'a']>=MOD){
                sum[str[i]-'a']-=MOD;
            }
        }
    }
    for(int i=0;i<26;++i){
        weight[i]=i;
        for(int j=1;j<L;++j){
            num[i][j]+=num[i][j-1]/26;
            num[i][j-1]%=26;
        }
        while(num[i][L]){
            num[i][L+1]=num[i][L]/26;
            num[i][L++]%=26;
        }
    }
    sort(weight,weight+26,cmp);
    int zero=-1;
    for(int i=0;i<26;++i){
        if(!head[weight[i]]){
            zero=weight[i];
            break;
        }
    }
    int ans=0,w=26;
    for(int i=25;i>=0;--i){
        if(zero!=weight[i]){
            ans+=(LL)(--w)*sum[weight[i]]%MOD;
            ans%=MOD;
        }
    }
    cout<<"Case #"<<++kase<<": "<<ans<<endl;
}


int main()
{
    ios::sync_with_stdio(false);
    int n;
    pwr[0]=1;
    for(int i=1;i<N;++i){
        pwr[i]=(LL)pwr[i-1]*26%MOD;
    }
    while(cin>>n){
        work(n);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值