2017 多校训练 1002 Balala Power!

Balala Power!

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


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
 

 

Recommend
liuyiding   |   We have carefully selected several similar problems for you:   6044  6043  6042  6041  6040 

 

/*
* @Author: Lyucheng
* @Date:   2017-07-26 11:03:09
* @Last Modified by:   Lyucheng
* @Last Modified time: 2017-07-26 17:24:29
*/
/*
 题意:给你n个字符串,只包含a~z的字符,你可以给字符赋值0~25,但是不同的字符的值不能相同,要求得到n个字符串的和最大

 思路:相当于n个26进制的数,然后考虑每个字符在每个字符串中位置对结果的贡献,贡献最多的为25,其次是24,以此类推,要
    注意的问题就是前导零的问题
*/
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>

#define LL long long
#define MAXN 100005
#define MAXK 26
const LL MOD=1e9+7;
using namespace std;

struct Node{
    int pos;//标记字符
    int num[MAXN];
    bool operator < (const Node & other) const {//重载小于号按照优先级排序
        for(int i=MAXN-1;i>0;i--){
            if(num[i]!=other.num[i])
                return num[i]<other.num[i];
        }
        return num[0]<other.num[0];
    }
}node[MAXK+1];
int n;
char str[MAXN];
bool vis[MAXK];//标记是否不能为第一个字符
int len;
int ca=1;

inline void init(){
    for(int i=0;i<MAXK;i++){
        node[i].pos=i;
        for(int j=0;j<MAXN;j++){
            node[i].num[j]=0;
        }
    }
    memset(vis,false,sizeof vis);
}

int main(){
    // freopen("in.txt", "r", stdin);
    // freopen("out.txt", "w", stdout);
    while(scanf("%d",&n)!=EOF){
        init();
        for(int i=0;i<n;i++){
            scanf("%s",str);
            len=strlen(str);
            if(len>1){//如果不是单个字符那么,第一个字符就不能为零
                vis[str[0]-'a']=true;
            }
            for(int j=0;j<len;j++){
                int x=str[j]-'a';//字符
                int y=len-j-1;//位置
                node[x].num[y]++;
                while(node[x].num[y]==MAXK){//满了26个了,不管是几都要进位
                    node[x].num[y++]=0;
                    node[x].num[y]++;
                }
            }
        }
        sort(node,node+MAXK);
        //找第一个能为零的字符
        int pos=-1;
        for(int i=0;i<MAXK;i++){//从优先级最小的开始找
            if(vis[node[i].pos]==false){
                pos=i;
                break;
            }
        }

        int POW=0;
        LL res=0;
        for(int i=0;i<MAXK;i++){
            if(i==pos){
                POW=0;
            }else if(i<pos){
                POW=i+1;
            }else{
                POW=i;
            }
            LL cur=0;
            for(int j=MAXN-1;j>=0;j--){
                cur = (cur * MAXK) % MOD;
                cur = (cur + (LL)node[i].num[j] * POW) % MOD;
            }
            res = (res + cur)%MOD;
        }
        printf("Case #%d: %lld\n",ca++,res);
    }    
    return 0;
}

 

转载于:https://www.cnblogs.com/wuwangchuxin0924/p/7240877.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值