[HDU2296]Ring
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
简单翻译一下:给出m个模式串(m≤100),每个模式串有一个权值。先要求构造一个长度不大于n(n≤50)的串使其包含的子模式串的权值和最大。若存在多解,则要求输出长度最小的,若仍存在多解则要求输出字典序最小的。
sol
DP出最大权值应该不难吧
要求字典序最小其实也很好办。开一个string记录路径就行了(string自带比较字典序)。
code
#include<iostream>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
const int N = 1500;
int T,n,m,tot,ch[26][N],fail[N],cnt[N],node[101],dp[55][N];
string path[55][N];
char s[N];
queue<int>Q;
void Insert(int k)
{
cin>>s;int l=strlen(s),x=0;
for (int i=0;i<l;i++)
{
if (!ch[s[i]-'a'][x]) ch[s[i]-'a'][x]=++tot;
x=ch[s[i]-'a'][x];
}
node[k]=x;
}
void Get_Fail()
{
for (int i=0;i<26;i++) if (ch[i][0]) Q.push(ch[i][0]);
while (!Q.empty())
{
int u=Q.front();Q.pop();
for (int i=0;i<26;i++)
if (ch[i][u]) fail[ch[i][u]]=ch[i][fail[u]],Q.push(ch[i][u]);
else ch[i][u]=ch[i][fail[u]];
cnt[u]+=cnt[fail[u]];
}
}
void DP()
{
memset(dp,-1,sizeof(dp));
dp[0][0]=0;path[0][0]="";
for (int i=0;i<n;i++)
for (int j=0;j<=tot;j++)
if (dp[i][j]!=-1)
for (int k=0;k<26;k++)
{
int u=ch[k][j];
if (dp[i][j]+cnt[u]>dp[i+1][u])
{
dp[i+1][u]=dp[i][j]+cnt[u];
path[i+1][u]=path[i][j]+(char)(k+'a');
}
else if (dp[i][j]+cnt[u]==dp[i+1][u])
{
string str=path[i][j]+(char)(k+'a');
if (str<path[i+1][u]) path[i+1][u]=str;
}
}
}
int main()
{
cin>>T;
while (T--)
{
cin>>n>>m;
for (int i=0;i<=tot;i++)
{
fail[i]=cnt[i]=0;
for (int j=0;j<26;j++) ch[j][i]=0;
}
tot=0;
for (int i=1;i<=m;i++) Insert(i);
for (int i=1,v;i<=m;i++) cin>>v,cnt[node[i]]+=v;
Get_Fail();
DP();
string str="";int ans=0,maxx=0;
for (int i=1;i<=n;i++)
for (int j=0;j<=tot;j++)
maxx=max(maxx,dp[i][j]);
for (int i=1;i<=n;i++)
{
for (int j=0;j<=tot;j++)
if (dp[i][j]>ans||(dp[i][j]==ans&&path[i][j]<str))
ans=dp[i][j],str=path[i][j];
if (ans==maxx) break;
}
cout<<str<<endl;
}
return 0;
}
[HDU2296]Ring的更多相关文章
- HDU2296 Ring —— AC自动机 + DP
题目链接:https://vjudge.net/problem/HDU-2296 Ring Time Limit: 2000/1000 MS (Java/Others) Memory Limit ...
- 【AC自动机】【动态规划】hdu2296 Ring
题解:http://www.cnblogs.com/swm8023/archive/2012/08/08/2627535.html 要输出路径,价值最大优先,价值相同的取长度较小者,仍相同取字典序较小 ...
- HDU-2296 Ring(AC自动机+DP)
题目大意:给出的m个字符串都有一个权值.用小写字母构造一个长度不超过n的字符串S,如果S包含子串s,则S获取s的权值.输出具有最大权值的最小字符串S. 题目分析:先建立AC自动机.定义状态dp(ste ...
- HDU2296 Ring(AC自动机 DP)
dp[i][j]表示行走i步到达j的最大值,dps[i][j]表示对应的串 状态转移方程如下: dp[i][chi[j][k]] = min(dp[i - 1][j] + sum[chi[j][k]] ...
- HDU2296 Ring(AC自动机+DP)
题目是给几个带有价值的单词.而一个字符串的价值是 各单词在它里面出现次数*单词价值 的和,问长度不超过n的最大价值的字符串是什么? 依然是入门的AC自动机+DP题..不一样的是这题要输出具体方案,加个 ...
- HDU2296——Ring(AC自动机+DP)
题意:输入N代表字符串长度,输入M代表喜欢的词语的个数,接下来是M个词语,然后是M个词语每个的价值.求字符串的最大价值.每个单词的价值就是单价*出现次数.单词可以重叠.如果不止一个答案,选择字典序最小 ...
- AC自动机基础知识讲解
AC自动机 转载自:小白 还可参考:飘过的小牛 1.KMP算法: a. 传统字符串的匹配和KMP: 对于字符串S = ”abcabcabdabba”,T = ”abcabd”,如果用T去匹配S下划线部 ...
- AC自动机题单
AC自动机题目 真的超级感谢xzy 真的帮到我很多 题单 [X] [luogu3808][模板]AC自动机(简单版) https://www.luogu.org/problemnew/show/P38 ...
- HDU 2296 Ring [AC自动机 DP 打印方案]
Ring Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submissio ...
随机推荐
- Jenkins持续集成-自动化部署脚本的实现
要实现Jenkins端的持续集成,其实在CI服务配置端很容易,难点呢?就是如何实现自动化的部署.我的脚本设计就是为了解决以下难题: 难点一.如何使得自动化部署脚本更通用 我用的脚本,依赖依赖一个配置文 ...
- Hive metastore源码阅读(三)
上次写了hive metastore的partition的生命周期,但是简略概括了下alter_partition的操作,这里补一下alter_partition,因为随着项目的深入,发现它涉及的地方 ...
- Sublime编辑器的使用
Sublime编辑器的使用 我的本地使用环境是Win7下安装的Sublime3编辑器(提取码:nzuw). 1. 新建index.html,在代码编辑区输入<html,然后按下Tab键,可以快速 ...
- maven配置全局的jdk和配置局部的jdk
配置全局的jdk需要修改maven的setting.xml文件 <profile> <id>jdk17</id> <activation> <ac ...
- 一致性哈希(附带C++实现)
在分布式集群中,对机器的添加删除,或者机器故障后自动脱离集群这些操作是分布式集群管理最基本的功能.如果采用常用的hash(object)%N算 法,那么在有机器添加或者删除后,就需要大范围的移动原有数 ...
- Maven json包找不到解决办法
在Maven中央仓库找到Maven的jar <dependency> <groupId>net.sf.json-lib</groupId> <artifact ...
- .NET Core阿里大于短信发送SDK修改以及使用
一.问题背景 继上次七牛云SDK的问题之后(参考:http://www.cnblogs.com/OMango/p/8447480.html),在发送短信的功能上又出现了问题,我们短信服务使用的是阿里大 ...
- CentOS7上安装FTP服务
---------------------------------------------------------------------------------------------------- ...
- iOS——系统提供的dispatch方法
// 后台执行: dispatch_async(dispatch_get_global_queue(0,0), ^{ // something }); // 主线程执行: dispatch_async ...
- 剑指offer第三天
21.栈的压入.弹出序列 输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否为该栈的弹出顺序.假设压入栈的所有数字均不相等.例如序列1,2,3,4,5是某栈的压入顺序,序列4,5,3, ...