2017 HDU 多校联合赛 Balala Power!

来源:互联网 发布:"网站seo推广方案ppt" 编辑:程序博客网 时间:2024/06/05 04:58

这里写图片描述

alented Mr.Tang has nn 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 10e9+7.

Input

The input contains multiple test cases.

For each test case, the first line contains one positive integers nn, the number of strings. (1≤n≤100000)(1≤n≤100000)
Each of the next nn lines contains a string sisi consisting of only lower case letters. (1≤|si|≤100000,∑|si|≤106)(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

题意:

给每一个字母赋值,形成一串26进制的数,不同字母不能赋同样的值,且不能出现前导0,从而使n串数字之和最大。

官方解析:

每个字符对答案的贡献都可以看作一个 26 进制的数字,问题相当于要给这些贡献加一个 0 到 25 的权重使得答案最大。最大的数匹配 25,次大的数匹配 24,依次类推。排序后这样依次贪心即可,唯一注意的是不能出现前导 0。

解析:

                   贪心算法:占高位的字母应该赋大值,出现次数多的字母应该赋大值。
  • 1. 开一个 26*100000Map 数组,用来存每一个字母在每一位上出现过的次数,表如下图。

    这里要有一个进位优化,即26个低位 a 等于1个高位 a 。

  • 2. 然后再开一个一位排序数组,rankk[ 30 ],用来对字母排序,排序规则为高位出现次数多的排在前

    面,例如:rankk[ 0 ] = 0 , rankk[ 1 ] = 3 , rankk[ 2 ] = 6 的话,那么意思就是 a 出现次数最多,d出现次数次多,g 出现次数最少。

  • 3. 解决前导 0 的方法,用一个 vis [ 30 ] 来记录当字符串长度超过 1 的时候出现在开头的字母( 即 不能赋值为 0 ) 然后当26个字母全部存在( 即 必须有一个字母赋值为 0 ),那么先找出一个符合规则 ( 即 出现次数少且不在字符串开头出现过 ) 的字母来赋值为 0 ,此字母标记为 last

  • 4.rankk[ 0 → 25 ]来赋值 25 , 24 , 23 …… 当碰到 last 字母时,将其单独赋值为 0 ,然后跳过它,继续向下赋值。

Map表

字母 个位 十位 百位 千位 万位 …… a 出现次数 出现次数 出现次数 出现次数 出现次数 b 出现次数 …… c 出现次数 …… d 出现次数 …… …… 出现次数 …… y 出现次数 …… z 出现次数
#include <cstdio>#include <algorithm>#include <cstring>#include <cmath>using namespace std;const int N = 100020;const int mod = 1e9 + 7;int n, maxlen,rankk[26],ant,sum[N],Map[26][N];long long power[N];bool vis[26];char str[N];bool cmp(int A, int B)                             ///rankk的排序规则{    for (int i = maxlen - 1 ; i >= 0 ; -- i)    {        if (Map[A][i] != Map[B][i])        {            return Map[A][i] < Map[B][i];        }    }    return 0;}void work(){    memset(Map, 0, sizeof(Map));    memset(vis, 0, sizeof(vis));    memset(sum, 0, sizeof(sum));    maxlen = 0;    for (int i = 0 ; i < n ; ++ i)    {        scanf("%s", str);        int len = strlen(str);        if (len > 1)        {            vis[str[0] - 'a'] = 1;                               ///记录不能赋值0的字母        }        reverse(str, str + len);        for (int j = 0 ; j < len ; ++ j)        {            ++Map[str[j] - 'a'][j];                                     ///统计每个字母在每一位上出现过的次数            sum[str[j] - 'a']=(sum[str[j] - 'a']+power[j])%mod;         ///sum用来统计某个字母有多少个(十进制)        }        maxlen = max(maxlen, len);    }    for (int i = 0 ; i < 26 ; ++ i)    {        for (int j = 0 ; j < maxlen ; ++ j)        {            Map[i][j + 1] += Map[i][j] / 26;                          ///优化进位操作(26个低位等于1个高位)            Map[i][j] %= 26;                                                  }        while (Map[i][maxlen])        {            Map[i][maxlen + 1] += Map[i][maxlen] / 26;            Map[i][maxlen ++] %= 26;        }        rankk[i] = i;    }    sort(rankk, rankk + 26, cmp);    int last = -1;    for (int i = 0 ; i < 26 ; ++ i)    {        if (!vis[rankk[i]])        {            last = rankk[i];                                     ///寻找赋值为0的字母            break;        }    }    int answer = 0, num = 25;    for (int i = 25 ; i >= 0 ; -- i)    {        if (rankk[i] != last)        {            answer=(answer+(long long)(num--)*sum[rankk[i]]%mod)%mod;                   ///最终计算总和        }    }    printf("Case #%d: %d\n", ++ant, answer);}int main(){    ant = 0;    power[0] = 1;    for (int i = 1 ; i < N ; ++ i)                         ///将26的n次幂打表备用    {        power[i] =power[i - 1] * 26 % mod;    }    while (~scanf("%d", &n))    {        work();    }    return 0;}