Problem Description
For the hope of a forever love, Steven is planning to send a ring to Jane with a romantic string engraved on. The string's length should not exceed N. The careful Steven knows Jane so deeply that he knows her favorite words, such as "love", "forever". Also, he knows the value of each word. The higher value a word has the more joy Jane will get when see it.
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.

 
Input
The input consists of several test cases. The first line of input consists of an integer T, indicating the number of test cases. Each test case starts with a line consisting of two integers: N, M, indicating the string's length and the number of Jane's favorite words. Each of the following M lines consists of a favorite word Si. The last line of each test case consists of M integers, while the i-th number indicates the value of Si.
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'.

 
Output
For each test case, output the string to engrave on a single line.
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.

 
Sample Input
2
7 2
love
ever
5 5
5 1
ab
5
 
Sample Output
lovever
abab
 
 
要求构造一个字符串使其包含的给定子串权值最大。
dp维护最大价值在AC自动机里跑就好啦
#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的更多相关文章

  1. HDU 2296 Ring (AC自动机+DP)

    Ring Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  2. HDU 2296 Ring [AC自动机 DP 打印方案]

    Ring Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submissio ...

  3. HDU 2296 Ring ( Trie图 && DP && DP状态记录)

    题意 : 给出 m 个单词,每一个单词有一个权重,如果一个字符串包含了这些单词,那么意味着这个字符串拥有了其权重,问你构成长度为 n 且权重最大的字符串是什么 ( 若有权重相同的,则输出最短且字典序最 ...

  4. HDU 2296 Ring -----------AC自动机,其实我想说的是怎么快速打印字典序最小的路径

    大冥神的代码,以后能贴的机会估计就更少了....所以本着有就贴的好习惯,= =....直接贴 #include <bits/stdc++.h> using LL = long long ; ...

  5. hdu 2296 aC自动机+dp(得到价值最大的字符串)

    Ring Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  6. Ring - HDU 2296(自动机+dp)

    题目大意:斯蒂文想送给他女盆友一个戒指,并且他想在戒指上刻一些字,他非常了解他女盆友喜欢什么单词,比如"love""forvevr"....并且他还把女盆友喜欢 ...

  7. Ring HDU - 2296 AC自动机+简单DP和恶心的方案输出

    题意: 就是现在给出m个串,每个串都有一个权值,现在你要找到一个长度不超过n的字符串, 其中之前的m个串每出现一次就算一次那个字符串的权值, 求能找到的最大权值的字符串,如果存在多个解,输出最短的字典 ...

  8. HDU 1016Prime Ring Problem

    http://acm.hdu.edu.cn/showproblem.php?pid=1016 题意:输入一个数,给出符合要求的素数环. 经典的dfs遍历. #include<iostream&g ...

  9. hdu Prime Ring Problem

    简单的dfs,貌似这道题用暴力枚举就可以了,毕竟数据开的是比较小的. #include"iostream" #include"algorithm" #inclu ...

随机推荐

  1. iOS 写给iOS开发者的React Native学习路线(转)

    我是一名iOS开发者,断断续续一年前开始接触React Native,最近由于工作需要,专职学习React Native也有一个多月了.网络上知识资源非常的多,但能让人豁然开朗.迅速学习的还是少数,我 ...

  2. xml生成方式二(Xml序列化器XmlSerializer)

    一.andoirdAPI提供了xml生成和解析的API: XmlSerializer xs = Xml.newSerializer();和XmlPullParser xmlPullParser = X ...

  3. GAME——转圈游戏

    我们在生命的路上常常绝望 大概是因为弯路走了太多 脚上的泡被磨起又磨破 像我们所有的幻想与梦 起起落落. 所以说 我这道题考场上面和题解想得一模一样啊啊啊啊啊啊啊啊啊啊!!!!!! 但是就是打复杂了啊 ...

  4. tee 命令详解

    作用:将数据重定向到文件,另一方面还可以提供一份重定向数据的副本作为后续命令的stdin . 简单的说就是把数据重定向给文件和屏幕上. 注意:存在缓存机制,每1024 字节输出一次, 若从管道接受数据 ...

  5. rmdir 命令详解

    rmdir  作用:  用来删除空目录, 当目录不再被使用时, 或者磁盘空间已达到使用限定值, 就需要删除失去价值的目录. 利用rmdir 命令可以从一个目录中删除一个或多个空的子目录. 该命令从一个 ...

  6. ABP 教程文档 1-1 手把手引进门之 AngularJs, ASP.NET MVC, Web API 和 EntityFramework(官方教程翻译版 版本3.2.5)含学习资料

    本文是ABP官方文档翻译版,翻译基于 3.2.5 版本 转载请注明出处:http://www.cnblogs.com/yabu007/  谢谢 官方文档分四部分 一. 教程文档 二.ABP 框架 三. ...

  7. find 命令的误差估值与单位调整

    一.命令简介 find 命令的 -size 参数 单位b(不是byte而是block).c.w.k.M.G.默认是单位b ,也就是1block = 512byte = 0.5kb (文件系统ext4) ...

  8. JAVA NIO学习三:NIO 的非阻塞式网络通信

    紧接着上一章,我们继续来研究NIO,上一章中我们讲了NIO 中最常见的操作即文件通道的操作,但实际上NIO的主要用途还是在于网络通信,那么这个时候就会涉及到选择器,这一章我们就会对其进行讲解操作. 一 ...

  9. npm lodash

    在数据操作时,Lodash 就是我的弹药库,不管遇到多复杂的数据结构都能用一些函数轻松拆解. ES6 中也新增了诸多新的对象函数,一些简单的项目中 ES6 就足够使用了,但还是会有例外的情况引用了少数 ...

  10. 5 秒创建 k8s 集群 - 每天5分钟玩转 Docker 容器技术(115)

    据说 Google 的数据中心里运行着超过 20 亿个容器,而且 Google 十年前就开始使用容器技术. 最初,Google 开发了一个叫 Borg 的系统(现在命令为 Omega)来调度如此庞大数 ...