HDU 6034 Balala Power!【贪心】

来源:互联网 发布:java连接sqlserver代码 编辑:程序博客网 时间:2024/06/07 06:20

Balala Power!

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


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
 

Source
2017 Multi-University Training Contest - Team 1
 

题解;

每个字符对答案的贡献都可以看作一个 26 进制的数字,问题相当于要给这些贡献加一个 0 到 25 的权重使得答案最大。最大的数匹配 25,次大的数匹配 24,

依次类推。排序后这样依次贪心即可,唯一注意的是不能出现前导 0。


#include<iostream>#include<cstdio>#include<cstring>#include<string>#include<cmath>#include<queue>#include<stack>#include<vector>#include<map>#include<set>#include<algorithm>using namespace std;#define ll long long#define ms(a,b)  memset(a,b,sizeof(a))#define maxn 510const int M=1e6+10;const int inf=0x3f3f3f3f;const int mod=1e9+7;const double eps=1e-10;ll n,m;int num[26][M];int power[M],sum[M];bool ban[26];char s[M];int a[26];int L;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 solve(){    ms(ban,0);    ms(sum,0);    ms(num,0);    L=0;    for(int i=0;i<n;i++){        scanf("%s",s);        int len=strlen(s);        if(len>1)ban[s[0]-'a']=1;        reverse(s,s+len);        for(int j=0;j<len;j++){            num[s[j]-'a'][j]++;            sum[s[j]-'a']+=power[j];            sum[s[j]-'a']%=mod;        }        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;            L++;        }        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 ans=0,x=25;    for(int i=25;i>=0;i--){        if(a[i]!=zero){            ans+=(ll)(x--)*sum[a[i]]%mod;            ans%=mod;        }    }    static int cas=0;    printf("Case #%d: %d\n",++ cas,ans);}int main(){    power[0]=1;    for(int i=1;i<=M;i++){        power[i]=(ll)power[i-1]*26%mod;    }    while(~scanf("%d",&n)){        solve();    }    return 0;}


原创粉丝点击