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
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
Input
The input contains multiple test cases.
For each test case, the first line contains one positive integersn , the number of strings. (1≤n≤100000)
Each of the nextn lines contains a string si consisting of only lower case letters. (1≤|si|≤100000,∑|si|≤106)
For each test case, the first line contains one positive integers
Each of the next
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
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;}
阅读全文
0 0
- hdu 6034 Balala Power!(贪心)
- HDU 6034 Balala Power!【贪心】
- HDU 6034 Balala Power!【贪心】
- HDU 6034 Balala Power! 排序 贪心
- HDU 6034 Balala Power! (贪心)
- HDU 6034 Balala Power!(贪心)
- HDU 6034 Balala Power!(贪心)
- HDU 6034 Balala Power!(贪心)
- HDU 6034 Balala Power!(贪心)
- HDU-6034 Balala Power!
- [HDU]-6034 Balala Power!
- HDU 6034 Balala Power!
- hdu 6034 Balala Power!
- 【HDU 6034 Balala Power!】
- hdu 6034 Balala Power!
- HDU 6034 Balala Power!
- HDU 6034 Balala Power!
- hdu-6034-Balala Power!
- hdu 5919 dfs前序遍历计数 笛卡尔树
- (hdu2588)GCD(欧拉函数)
- 【面经笔记】TCP
- linux 安装nginx
- maven配置文件pom.xml详解
- HDU 6034 Balala Power!【贪心】
- lanqiao 小白算法练习 合并石子 区间dp
- 剑指offer面试题[59]-对称的二叉树
- JVM运行时内存划分
- Ubuntu install Google-Chrome
- 中序线索化链表
- codeforces 813C The Tag Game
- 在textview周围画图
- mysql数据库索引的创建语句