HDU 2017多校联赛-1012 Balala Power!

来源:互联网 发布:好玩的java源代码 编辑:程序博客网 时间:2024/06/07 17:41

问题描述


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 1e9+7.

输入
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)
输出
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.
样例输入
1
a
2
aa
bb
3
a
ba
abc
样例输出
Case #1: 25
Case #2: 1323
Case #3: 18221

这题 我有点蒙

大佬代码

#include <iostream>#include <stdio.h>#include <string>#include <string.h>#include <algorithm>using namespace std;typedef long long LL;const int maxn = 100010;const int mod = 1e9+7;  ///模值LL num[30][maxn];      ///num[i][j]代表i+'a'这个字母在第j位出现的次数int isok[30];           ///用来存放i+'a'这个字母是否可以是0,可以设置为0,不可以设置为1int power[30];          ///存放对应的权值int a[30];int maxbit;             ///每租数据的最高位。string str[maxn];       ///存放字符串///计算字符串的值LL calculate(string s){    LL ans = 0;    for(int i = 0; i < s.length(); i++)        ans = (ans*26+power[s[i]-'a'])%mod;    return ans;}bool cmp(int num1,int num2){    for(int i = maxbit; i >= 0; i--)    {        if(num[num1][i] != num[num2][i])            return num[num1][i] < num[num2][i];    }    return 0;}int main(){    int Case = 0,n;    while(~scanf("%d",&n))    {        maxbit = -1;        memset(num,0,sizeof(num));        memset(isok,0,sizeof(isok));        for(int i = 0; i < n; i++)        {            cin>>str[i];            int len = str[i].length();            if(len > 1)            {                isok[str[i][0]-'a'] = 1;  ///首字母不能作为前导            }            for(int j = len-1; j >= 0; j--)            {                int k = str[i][j]-'a';                num[k][len-1-j]++;            }            maxbit = max(maxbit,len-1);        }        for(int i = 0; i < 26; i++)        {            for(int j = 0; j < maxbit; j++)            {                num[i][j+1] += num[i][j]/26;                num[i][j] = num[i][j]%26;            }            a[i] = i;        }        sort(a,a+26,cmp);  ///优先级从小到大排序。        int not_ok=-1;        for(int i = 0; i < 26; i++)        {            if(!isok[a[i]])            {                not_ok = a[i];                break;            }        }        int num = 25;        for(int i = 25; i>=0 ;i--)        {            if(not_ok==a[i])            {                power[a[i]] = 0;            }            else            {                power[a[i]] = num;                num--;            }        }        LL ans = 0;        for(int i = 0; i < n; i++)        {            ans = (ans + calculate(str[i]))%mod;        }        printf("Case #%d: %lld\n",++Case,ans);    }    return 0;}

更加大佬的代码

#include <bits/stdc++.h>using namespace std;typedef long long LL;const int N = 100020;const int Q = 1e9 + 7;int n , L;int num[26][N];int power[N] , sum[N];bool ban[26];char str[N];int a[26];bool cmp(int A , int B) {    for (int i = L - 1 ; i >= 0 ; -- i) {        if (num[A][i] != num[B][i]) {            return num[A][i] < num[B][i];        }    }    return 0;}void work() {    memset(num , 0 , sizeof(num));    memset(ban , 0 , sizeof(ban));    memset(sum , 0 , sizeof(sum));    L = 0;    for (int i = 0 ; i < n ; ++ i) {        scanf("%s" , str);        int len = strlen(str);        if (len > 1) {            ban[str[0] - 'a'] = 1;        }        reverse(str , str + len);        for (int j = 0 ; j < len ; ++ j) {            ++ num[str[j] - 'a'][j];            sum[str[j] - 'a'] += power[j];            if (sum[str[j] - 'a'] >= Q) {                sum[str[j] - 'a'] -= Q;            }        }        L = max(L , len);    }    for (int i = 0 ; i < 26 ; ++ i) {        for (int j = 0 ; j < L ; ++ j) {            num[i][j + 1] += num[i][j] / 26;            num[i][j] %= 26;        }        while (num[i][L]) {            num[i][L + 1] += num[i][L] / 26;            num[i][L ++] %= 26;        }        a[i] = i;    }    sort(a , a + 26 , cmp);    int zero = -1;    for (int i = 0 ; i < 26 ; ++ i) {        if (!ban[a[i]]) {            zero = a[i];            break;        }    }    int res = 0 , x = 25;    for (int i = 25 ; i >= 0 ; -- i) {        if (a[i] != zero) {            res += (LL)(x --) * sum[a[i]] % Q;            res %= Q;        }    }    static int ca = 0;    printf("Case #%d: %d\n" , ++ ca , res);}int main() {    power[0] = 1;    for (int i = 1 ; i < N ; ++ i) {        power[i] = (LL)power[i - 1] * 26 % Q;    }    while (~scanf("%d" , &n)) {        work();    }    return 0;}
原创粉丝点击