HDU 6034---Balala Power!(搜索+贪心)
2018-06-17 22:10:12来源:未知 阅读 ()
题目链接
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.
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)
#include <iostream> #include <algorithm> #include <stdio.h> #include <string.h> #include <stdlib.h> using namespace std; typedef long long LL; const int MAXN=1e5+15; const LL MOD = 1e9+7; char str[MAXN]; int k[30][MAXN]; int mp[30]; int ans[30]; int temp; int check(int a[30]) { int num=0; for(int i=0;i<30;i++) num+=a[i]; return num; } int pp() { for(int i=0;i<30;i++) if(mp[i]==1) return i; } void dfs(int dep,int o[]) { if(dep<0) return ; int maxn=-1; int oo[30] , ct=0,pos=-1; memset(oo,0,sizeof(oo)); for(int i=0;i<=25;i++) { if(maxn<k[i][dep]&&o[i]==1) maxn=k[i][dep]; } for(int i=0;i<=25;i++) { if(maxn==k[i][dep]&&o[i]==1) { oo[i]=1; o[i]=0; ct++; pos=i; } } if(ct==0) return ; if(ct==1){ if(check(mp)==1&&pos==pp()) ans[pos]=25; else ans[pos]=temp++; mp[pos]=0; } else if(dep==0){ for(int i=0;i<26;i++) { if(maxn==k[i][dep]&&oo[i]==1) { if(check(mp)==1&&i==pp()) ans[i]=25; else ans[i]=temp++; mp[i]=0; } } } else dfs(dep-1,oo); if(check(o)>0) dfs(dep,o); } LL Pow(LL a, LL b){ LL ans = 1; while(b){ if(b & 1) ans = (ans * a) % MOD; b>>=1; a = (a * a ) % MOD; } return ans; } int main() { int T,n,L,l,cas=1; int one[30]; while(scanf("%d",&n)!=-1) { L=0; for(int i=0;i<26;i++) one[i]=1,mp[i]=1; memset(k,0,sizeof(k)); ///memset(ans,0,sizeof(ans)); for(int i=0;i<n;i++) { scanf("%s",str); mp[str[0]-'a']=0; l=strlen(str); if(l>L) L=l; for(int j=0;j<l;j++) k[str[j]-'a'][l-j-1]++; } for(int i=0;i<26;i++) { for(int j=0;j<L+10;j++) { k[i][j+1]+=k[i][j]/26; k[i][j]=k[i][j]%26; } } temp=0; dfs(L+10,one); ///for(int i=0;i<26;i++) cout<<ans[i]<<" "; cout<<endl; LL res=0; for(int i=0;i<26;i++) { for(int j=0;j<L+10;j++) { ///res+=(25-ans[i])*Pow(26,j)*k[i][j]; res = (res+((25-ans[i])*Pow(26,j) % MOD)*k[i][j]%MOD)%MOD; } } printf("Case #%d: %lld\n",cas++,res); } return 0; }
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- HDU-2955-Robberies(0-1背包) 2020-03-30
- hdu1455 拼木棍(经典dfs) 2020-02-29
- anniversary party_hdu1520 2020-02-16
- hdu1062 text reverse 2020-01-27
- hdu4841 2020-01-26
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash