题目链接:https://vjudge.net/problem/HDU-2296

Ring

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4429    Accepted Submission(s): 1474

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

Hint

Sample 1: weight(love) = 5, weight(ever) = 5, so weight(lovever) = 5 + 5 = 10
Sample 2: weight(ab) = 2 * 5 = 10, so weight(abab) = 10

 
Source

题意:

给出m个单词以及每个单词的价值,问长度不超过n的字符串价值最大是多少?求出这个字符串。

如果价值不同,取价值最大的;

如果价值相同,则取长度最短的;

如果价值相同,且长度相同,取字典序最小的。

题解:

1.把m个单词插入AC自动机中。

2.设dp[i][j]为:长度为i,且到达的状态为j(自动机上的状态)的最大价值。设path[i][j](string类型)为:长度为i,且到达的状态为j的最大价值的路径。

3.状态转移详情请看代码及注释。

代码如下:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const double EPS = 1e-;
const int INF = 2e9;
const LL LNF = 2e18;
const int MOD = 1e9+;
const int MAXN = 5e3+; int dp[][MAXN];
string path[][MAXN]; struct Trie
{
int sz, base;
int next[MAXN][], fail[MAXN], end[MAXN];
char ch[MAXN]; //用于记录结点(状态)上的字符
int root, L;
int newnode()
{
for(int i = ; i<sz; i++)
next[L][i] = -;
end[L++] = ;
return L-;
}
void init(int _sz, int _base)
{
sz = _sz;
base = _base;
L = ;
root = newnode();
}
void insert(char buf[], int val)
{
int len = strlen(buf);
int now = root;
for(int i = ; i<len; i++)
{
if(next[now][buf[i]-base] == -) next[now][buf[i]-base] = newnode();
now = next[now][buf[i]-base];
ch[now] = buf[i];
}
end[now] += val; // end用于记录值
}
void build()
{
queue<int>Q;
fail[root] = root;
for(int i = ; i<sz; i++)
{
if(next[root][i] == -) next[root][i] = root;
else fail[next[root][i]] = root, Q.push(next[root][i]);
}
while(!Q.empty())
{
int now = Q.front();
Q.pop();
// end[now] += end[fail[now]]; //不用累加,因为在DP的时候会累加
for(int i = ; i<sz; i++)
{
if(next[now][i] == -) next[now][i] = next[fail[now]][i];
else fail[next[now][i]] = next[fail[now]][i], Q.push(next[now][i]);
}
}
} void query(int n)
{
for(int i = ; i<=n; i++)
for(int j = ; j<L; j++)
dp[i][j] = -INF; dp[][root] = ; path[][root] = "";
for(int i = ; i<=n; i++)
for(int j = ; j<L; j++)
for(int k = ; k<sz; k++)
{
int newi = i+;
int newj = next[j][k];
/* 可更新的两种情况:
情况1:新串价值<旧串价值
情况2:新串价值=旧串价值 且 新串字典序<旧串字典序
注:因为长度都是i+1,所以不用考虑长度的情况
*/
if(dp[newi][newj]<dp[i][j]+end[newj] ||
(dp[newi][newj]==dp[i][j]+end[newj]&& path[newi][newj]>path[i][j]+ch[newj]))
{
dp[newi][newj] = dp[i][j]+end[newj];
path[newi][newj] = path[i][j]+ch[newj];
}
} int posi = , posj = , maxx = -;
for(int i = ; i<=n; i++)
for(int j = ; j<L; j++)
if( dp[i][j]>maxx ||
(dp[i][j]==maxx&&path[i][j].size()<path[posi][posj].size())||
(dp[i][j]==maxx&&i==posi&&path[i][j]<path[posi][posj]))
{
/* 可更新的三种情况:
情况1:新串价值<旧串价值
情况2:新串价值=旧串价值 且 新串长度<旧串长度
情况2:新串价值=旧串价值 且 新串长度=旧串长度 且 新串字典序<旧串字典序
*/
maxx = dp[i][j];
posi = i;
posj = j;
}
cout<<path[posi][posj]<<endl;
}
}; Trie ac;
char buf[][];
int H[];
int main()
{
int T, n, m;
scanf("%d", &T);
while(T--)
{
ac.init(, 'a');
scanf("%d%d", &n,&m);
for(int i = ; i<=m; i++) scanf("%s", buf[i]);
for(int i = ; i<=m; i++) scanf("%d", &H[i]);
for(int i = ; i<=m; i++) ac.insert(buf[i], H[i]); ac.build();
ac.query(n);
}
return ;
}

HDU2296 Ring —— AC自动机 + DP的更多相关文章

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

    题目大意:给出的m个字符串都有一个权值.用小写字母构造一个长度不超过n的字符串S,如果S包含子串s,则S获取s的权值.输出具有最大权值的最小字符串S. 题目分析:先建立AC自动机.定义状态dp(ste ...

  2. 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]] ...

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

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

  4. HDU2296 Ring(AC自动机+DP)

    题目是给几个带有价值的单词.而一个字符串的价值是 各单词在它里面出现次数*单词价值 的和,问长度不超过n的最大价值的字符串是什么? 依然是入门的AC自动机+DP题..不一样的是这题要输出具体方案,加个 ...

  5. HDU2296——Ring(AC自动机+DP)

    题意:输入N代表字符串长度,输入M代表喜欢的词语的个数,接下来是M个词语,然后是M个词语每个的价值.求字符串的最大价值.每个单词的价值就是单价*出现次数.单词可以重叠.如果不止一个答案,选择字典序最小 ...

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

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

  7. 对AC自动机+DP题的一些汇总与一丝总结 (2)

    POJ 2778 DNA Sequence (1)题意 : 给出m个病毒串,问你由ATGC构成的长度为 n 且不包含这些病毒串的个数有多少个 关键字眼:不包含,个数,长度 DP[i][j] : 表示长 ...

  8. POJ1625 Censored!(AC自动机+DP)

    题目问长度m不包含一些不文明单词的字符串有多少个. 依然是水水的AC自动机+DP..做完后发现居然和POJ2778是一道题,回过头来看都水水的... dp[i][j]表示长度i(在自动机转移i步)且后 ...

  9. HDU2457 DNA repair(AC自动机+DP)

    题目一串DNA最少需要修改几个基因使其不包含一些致病DNA片段. 这道题应该是AC自动机+DP的入门题了,有POJ2778基础不难写出来. dp[i][j]表示原DNA前i位(在AC自动机上转移i步) ...

随机推荐

  1. 【FUN】——英文版面青年教育网站策划&GUI设计

    写在前面:这个教育网页一共分为四个页面,首页.课程.活动.空间.是我在学习网页设计与策划的时候作为知识应用练习做的,主要使用Photoshop软件设计构图,其中图片素材与部分灵感来源于网络. 一.网站 ...

  2. es6 - 模板

    'use strict'; // es5 let name = 'mrs'; let qb = 20; function logs() { return 'goods!'; } let html = ...

  3. 环信ONE SDK架构介绍

    环信ONE SDK架构介绍 摘要 环信即时通讯SDK自2014年6月正式公布2.0版本号至今已走过一个年头.从主要的单聊功能,到群聊功能,再到聊天室的实现.SDK无论是功能.稳定性,还是易集成性都在一 ...

  4. Scanner遇上UnmappableCharacterException

    上周末的时候.朋友约好去KTV,鉴于我这样的不怎么听歌的孩子伤不起啊,灵机一动就把我的酷狗歌单导出来了,XML文件嘛,内容太多,我仅仅想要歌名足已. 于是写了一个java去输出歌名.     岂料我受 ...

  5. apue学习笔记(第四章 文件和目录)

    本章将描述文件系统的其他特性和文件的性质. 函数stat.fstat.fstatat和lstat #include <sys/stat.h> int stat(const char *re ...

  6. Spring学习八----------Bean的配置之Resources

    © 版权声明:本文为博主原创文章,转载请注明出处 Resources 针对于资源文件的统一接口 -UrlResource:URL对应的资源,根据一个URL地址即可创建 -ClassPathResour ...

  7. aar格式

    aar包是Android Library Project的二进制公布包. 文件的扩展名是aar,并且maven包类型也应该是aar. 只是这文件本身就是一个简单的zip文件.里面有例如以下的内容: / ...

  8. 想全面理解JWT?一文足矣!

    有篇关于JWT的文章,叫"JWT: The Complete Guide to JSON Web Tokens",写得全面细致.为了自己能更清晰理解并惠及更多人,我把它大致翻译了过 ...

  9. ubuntu16.04--在标题栏显示网速

    有时感觉网络失去响应,就通过Ubuntu 14.04自带的系统监视器程序来查看当前网速,但是这样很不方便,遂打算让网速显示在标题栏,那样就随时可直观的看到.一番搜索尝试后,成功实现!同时也实现了CPU ...

  10. WARN util.NativeCodeLoader: Unable to load native-hadoop l... using builtin-java classes where applicable(附编译脚本)

    WARN util.NativeCodeLoader: Unable to load native-hadoop l... using builtin-java classes where appli ...