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 ...
随机推荐
- 【python】字典dict
- MySQL操作时间的函数集
求两个Timestamp之间的秒差值: select TIMESTAMPDIFF(SECOND,TIMESTAMP("2017-03-01 07:58:20"),timestamp ...
- JavaSE项目之聊天室
引子: 当前,互联网 体系结构的参考模型主要有两种,一种是OSI参考模型,另一种是TCP/IP参考模型. 一.OSI参考模型,即开放式通信系统互联参考模型(OSI/RM,Open Systems In ...
- [置顶]
Xamarin android 调用Web Api(ListView使用远程数据)
xamarin android如何调用sqlserver 数据库呢(或者其他的),很多新手都会有这个疑问.xamarin android调用远程数据主要有两种方式: 在Android中保存数据或调用数 ...
- bzoj 4310: 跳蚤
Description 很久很久以前,森林里住着一群跳蚤.一天,跳蚤国王得到了一个神秘的字符串,它想进行研究. 首先,他会把串分成不超过 k 个子串,然后对于每个子串 S,他会从S的所有子串中选择字典 ...
- PHP-无限级分类(迭代法创建)
$area = array( array('id'=>1,'name'=>'安徽','parent'=>0), array('id'=>2,'name'=>'海淀','p ...
- Golang中的坑二
Golang中的坑二 for ...range 最近两周用Golang做项目,编写web服务,两周时间写了大概五千行代码(业务代码加单元测试用例代码).用Go的感觉很爽,编码效率高,运行效率也不错,用 ...
- 如何更改 iOS 和安卓浏览器上的 input[type="radio"] 元素的默认样式?
Safari 上的默认样式是这样的, 背景颜色可以使用background-color改变,但中间那个点始终无法去掉. 我查了一些jQuery插件,如iCheck.js,但是那说明写得我都看不明白,根 ...
- Python学习_06_文件、IO
文件对象 python中的文件操作和c语言比较类似,包括一些缓冲.偏移量的方式. 文件对象可以通过open().file()两个内建方法创建,两个方法并没有什么不同,使用方法和c语言中的fopen() ...
- Head First设计模式之迭代器模式
一.定义 提供一种方法顺序访问一个聚合对象中各个元素, 而又无须暴露该对象的内部表示: 主要解决:不同的方式来遍历整个整合对象. 何时使用:遍历一个聚合对象. 如何解决:把在元素之间游走的责任交给迭代 ...