HDU 2296:Ring
The weight of a word is defined as its appeared times in the romantic string multiply by its value, while the weight of the romantic string is defined as the sum of all words' weight. You should output the string making its weight maximal.
Technical Specification
1. T ≤ 15
2. 0 < N ≤ 50, 0 < M ≤ 100.
3. The length of each word is less than 11 and bigger than 0.
4. 1 ≤ Hi ≤ 100.
5. All the words in the input are different.
6. All the words just consist of 'a' - 'z'.
If there's more than one possible answer, first output the shortest one. If there are still multiple solutions, output the smallest in lexicographically order.
The answer may be an empty string.
#include<iostream>
#include<queue>
#include<string>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std; const int LO=,NU=;
inline int f(char u){
return u-'a';
}
struct tree{
int f;
int q;
int t[LO];
int v[LO];
}t[NU];
int n,m,p,num,w;
bool us[NU];
char s[][];
queue <int> q;
int dp[][NU];
string ss[][NU],str;
inline int dfs(int x){
if (us[x]) return t[x].q;
us[x]=;
if (!x) return ;
return t[x].q+=dfs(t[x].f);
}
inline void in(int x){
int p=,l,m=strlen(s[x]);
for (register int i=;i<m;i++){
l=f(s[x][i]);
if (!t[p].t[l]) t[p].t[l]=++num;
p=t[p].t[l];
}
t[p].q=w;
}
inline void mafa(){
register int i;int k,p;
q.push();t[].f=;
while(!q.empty()){
k=q.front();q.pop();
for (i=;i<LO;i++)
if (t[k].t[i]){
p=t[k].f;
while((!t[p].t[i])&&p) p=t[p].f;
t[t[k].t[i]].f=(k==p)?:t[p].t[i];
q.push(t[k].t[i]);
}
}
}
int main(){
int tt;
scanf("%d",&tt);
register int i,j,k,l;int u;int ans;
while(tt--){
scanf("%d%d",&n,&m);
num=u=ans=;
for (i=;i<m;i++) scanf("%s",s[i]);
for (i=;i<m;i++){
scanf("%d",&w);
in(i);
}
mafa();
for (i=;i<=num;i++) us[i]=;
for (i=;i<=num;i++) if (!us[i]) dfs(i);
for (i=;i<=num;i++)
for (j=;j<LO;j++){
if (!t[i].t[j]){
u=t[i].f;
while(!t[u].t[j]&&u) u=t[u].f;
u=t[u].t[j];
}else u=t[i].t[j];
t[i].v[j]=u;
}
for (i=;i<=n;i++)
for (j=;j<=num;j++) dp[i][j]=-;
dp[][]=;ss[][]="";
for (i=;i<n;i++)
for (j=;j<=num;j++)
if (dp[i][j]!=-)
for (l=;l<LO;l++)
if ((dp[i+][t[j].v[l]]<t[t[j].v[l]].q+dp[i][j])||(dp[i+][t[j].v[l]]==t[t[j].v[l]].q+dp[i][j]&&ss[i+][t[j].v[l]]>ss[i][j]+(char)(l+'a')))
dp[i+][t[j].v[l]]=dp[i][j]+t[t[j].v[l]].q,ss[i+][t[j].v[l]]=ss[i][j]+(char)(l+'a');
ans=;str="";
for (i=;i<=n;i++)
for (j=;j<=num;j++) if (dp[i][j]>ans||(dp[i][j]==ans&&(str.size()>ss[i][j].size()||(str.size()==ss[i][j].size()&&str>ss[i][j])))) ans=dp[i][j],str=ss[i][j];
cout<<str<<endl;
for (i=;i<=num;i++)
for (j=;j<LO;j++) t[i].t[j]=t[i].v[j]=;
for (i=;i<=num;i++) t[i].q=t[i].f=;
}
}
HDU 2296:Ring的更多相关文章
- HDU 2296 Ring (AC自动机+DP)
Ring Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- HDU 2296 Ring [AC自动机 DP 打印方案]
Ring Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submissio ...
- HDU 2296 Ring ( Trie图 && DP && DP状态记录)
题意 : 给出 m 个单词,每一个单词有一个权重,如果一个字符串包含了这些单词,那么意味着这个字符串拥有了其权重,问你构成长度为 n 且权重最大的字符串是什么 ( 若有权重相同的,则输出最短且字典序最 ...
- HDU 2296 Ring -----------AC自动机,其实我想说的是怎么快速打印字典序最小的路径
大冥神的代码,以后能贴的机会估计就更少了....所以本着有就贴的好习惯,= =....直接贴 #include <bits/stdc++.h> using LL = long long ; ...
- hdu 2296 aC自动机+dp(得到价值最大的字符串)
Ring Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- Ring - HDU 2296(自动机+dp)
题目大意:斯蒂文想送给他女盆友一个戒指,并且他想在戒指上刻一些字,他非常了解他女盆友喜欢什么单词,比如"love""forvevr"....并且他还把女盆友喜欢 ...
- Ring HDU - 2296 AC自动机+简单DP和恶心的方案输出
题意: 就是现在给出m个串,每个串都有一个权值,现在你要找到一个长度不超过n的字符串, 其中之前的m个串每出现一次就算一次那个字符串的权值, 求能找到的最大权值的字符串,如果存在多个解,输出最短的字典 ...
- HDU 1016Prime Ring Problem
http://acm.hdu.edu.cn/showproblem.php?pid=1016 题意:输入一个数,给出符合要求的素数环. 经典的dfs遍历. #include<iostream&g ...
- hdu Prime Ring Problem
简单的dfs,貌似这道题用暴力枚举就可以了,毕竟数据开的是比较小的. #include"iostream" #include"algorithm" #inclu ...
随机推荐
- 507. Perfect Number
We define the Perfect Number is a positive integer that is equal to the sum of all its positive divi ...
- 视觉SLAM
SLAM:Simultaneous Localization And Mapping.中文:同时定位与地图重建. 它是指搭载特定传感器的主体,在没有实验先验信息的情况下,于运动过程中建立环境的模型,同 ...
- bzoj 4199 [NOI2015]寿司晚宴
Description 为了庆祝 NOI 的成功开幕,主办方为大家准备了一场寿司晚宴.小 G 和小 W 作为参加 NOI 的选手,也被邀请参加了寿司晚宴. 在晚宴上,主办方为大家提供了 n−1 种不同 ...
- RabbitMQ之消费者Demo(队列参数详细说明)
package com.jiefupay; import java.io.IOException; import java.util.HashMap; import java.util.Map; 8 ...
- apache泛域名解析
<VirtualHost *:80> DocumentRoot "E:\work\phpStudy\WWW\ncpx\web" ServerName ncp ...
- 3.sass的数据类型与函数
数据类型 在sass里有数字.字符串.列表.颜色等类型 在cmd里 输入 sass -i 就会进入到交互模式,输入的计算可以马上得到结果 type-of()可以用来得到数据类型,如: type-of( ...
- Centos6.8防火墙配置
1.基本操作 # 查看防火墙状态 service iptables status # 停止防火墙 service iptables stop # 启动防火墙 service iptables star ...
- 给yii架构的网站做Android客户端
网站未预留接口.给这类网站制作客户端就是模拟网页发送的post和get请求,然后获取服务器返回的数据. 1.在做客户端时先使用httpwatch分析浏览器发送的get和post请求的内容和URL.分析 ...
- Discuz uc.key泄露导致代码注入漏洞
漏洞描述:在Discuz中,uc_key是UC客户端与服务端通信的通信密钥,discuz中的/api/uc.php存在代码写入漏洞,导致黑客可写入恶意代码获取uckey,最终进入网站后台,造成数据泄漏 ...
- python 多进程开发与多线程开发
转自: http://tchuairen.blog.51cto.com/3848118/1720965 博文作者参考的博文: 博文1 博文2 我们先来了解什么是进程? 程序并不能单独运行,只有将程 ...