HDU 6034

来源:互联网 发布:淘宝客服的服务用语 编辑:程序博客网 时间:2024/05/21 00:45

Balala Power!

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


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
1a2aabb3abaabc
 

Sample Output
Case #1: 25Case #2: 1323Case #3: 18221

大意:给出的字符串,每个字符建立一种与0-25的对应关系。然后每个字符串看成是一个26进制的数。问能获得的数的总和的最大值。(最后对1e9+7取模)。

所有的字符串,无非就是每个字符的贡献乘上赋予的映射的权值。所以我们可以按照贡献排序。贡献其实就是跟位置有关的系数,我们把对应的位置记录下来,转换成一个26进制数。

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

#include <bits/stdc++.h>using namespace std;const int MAXN = 1e5+7;const long long mod = 1e9+7;int n;struct node{    int id;    int num[MAXN];    bool operator < (const node &a)const    {        for(int j = 100000; j >= 0; --j)if(num[j] != a.num[j])return num[j] > a.num[j];        return 0;    }}p[30];string s[MAXN];long long num[30];long long k[MAXN];bool ha[30];int main(){    ios::sync_with_stdio(false);    k[0] = 1;    for(int i = 1 ; i <= 100000; ++i)k[i] = (k[i-1]*26)%mod;    int ca = 0;    while(cin>>n)    {        for(int i = 0; i < 26; ++i)        {            for(int j=0; j<=100000; j++)p[i].num[j]=0;            p[i].id = i;            ha[i] = 0;        }        for(int i = 0; i < n; ++i)        {            cin>>s[i];            int l = s[i].size();            if(l > 1)ha[s[i][0]-'a'] = 1;            for(int ii = l-1,j=0; ii >=0; --ii,++j)            {                int t = s[i][ii]-'a';                p[t].num[j]++;            }        }        for(int i = 0; i < 26; ++i)        {            for(int j = 0; j <= 100000; ++j)            {                p[i].num[j+1] += p[i].num[j]/26;                p[i].num[j]%=26;            }        }        sort(p,p+26);        for(int i = 25; i >= 0; --i)num[p[25-i].id] = i;        int t = 25;        while(ha[p[t].id]&&t)        {            swap(num[p[t].id],num[p[t-1].id]);            t--;        }        long long ans = 0;        for(int i = 0; i < n; ++i)        {            int l = s[i].size();            for(int j = 0; j < l; ++j)            {                ans = (ans+num[s[i][j] -'a']*k[l-1-j]%mod)%mod;            }        }        cout<<"Case #"<<++ca<<": "<<ans<<endl;    }    return 0;}






原创粉丝点击