hdu 6034 B - Balala Power! 贪心
題目鏈接
http://acm.hdu.edu.cn/showproblem.php?pid=6034
題面描述
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.
輸入
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
題意
給你n個(gè)由26個(gè)字母組成的字符串,你現(xiàn)在要給每個(gè)字母用[0,25]賦值,不能要求有前導(dǎo)0,每個(gè)值對應(yīng)一個(gè)字母.
要求使得字符串組成的數(shù)字和最大。
題解
直接算每個(gè)字母的貢獻(xiàn),然后賦值就行,把0給最小的合法的字母即可。
代碼
#include<bits/stdc++.h> using namespace std; const int maxn = 1e5+7; const int mod = 1e9+7; struct node{int len[maxn];int w;int v;int L; }p[26]; int n; string s[maxn]; int flag[26]; int cas = 0; bool cmp(node A,node B){if(A.L!=B.L)return A.L<B.L;for(int i=A.L;i>=0;i--){if(A.len[i]==B.len[i])continue;return A.len[i]<B.len[i];}return 0; } int main(){while(cin>>n){cas++;memset(flag,0,sizeof(flag));for(int i=0;i<26;i++){for(int j=0;j<=p[i].L;j++){p[i].len[j]=0;}}for(int i=0;i<26;i++){p[i].v=0;p[i].w=i;p[i].L=-1;}for(int i=0;i<n;i++){cin>>s[i];if(s[i].size()>1){flag[s[i][0]-'a']++;}reverse(s[i].begin(),s[i].end());for(int j=0;j<s[i].size();j++){p[s[i][j]-'a'].len[j]++;}}for(int i=0;i<26;i++){for(int j=0;j<maxn-1;j++){if(p[i].len[j]>=26){int d = p[i].len[j]/26;p[i].len[j]%=26;p[i].len[j+1]+=d;}if(p[i].len[j]>0){p[i].L=max(p[i].L,j);}}}sort(p,p+26,cmp);for(int i=0;i<26;i++){p[i].v=i;}for(int i=0;i<26;i++){if(flag[p[i].w]&&p[i].v==0){swap(p[i].v,p[i+1].v);}else break;}long long ans = 0;long long value[26];for(int i=0;i<26;i++){value[p[i].w]=p[i].v;}for(int i=0;i<n;i++){long long now = 1;for(int j=0;j<s[i].size();j++){ans=(ans+(value[s[i][j]-'a']*now)%mod)%mod;now=(now*26)%mod;}}cout<<"Case #"<<cas<<": "<<ans<<endl;} }總結(jié)
以上是生活随笔為你收集整理的hdu 6034 B - Balala Power! 贪心的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Apache Kafka源码剖析:第5篇
- 下一篇: 跟踪Makefile输出调试信息