题目链接: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. BEGINNING SHAREPOINT&#174; 2013 DEVELOPMENT 第9章节--client对象模型和REST APIs概览 client对象模型(CSOM)基础

    BEGINNING SHAREPOINT® 2013 DEVELOPMENT 第9章节--client对象模型和REST APIs概览  client对象模型(CSOM)基础         在SP2 ...

  2. mongodb副本集的基础概念和各种机制

         从一开始我们就在讲如何使用一台服务器.一个mongod服务器进程,如果只用做学习和开发,这是可以的,但如果在生产环境中,这是很危险的,如果服务器崩溃了怎么办?数据库至少要一段时间不可用,如果 ...

  3. 微信小程序 - 考试状态不同显示

    未开考 .已交卷. 考试中 .考试结束 #ddd      #f00     #ff0    默认禁用色 禁用的button仅有style起作用,四个状态,通过wx:if ... elif ... e ...

  4. git 添加远程库

    1.登陆GitHub,然后,在右上角找到“Create a new repo”按钮,创建一个新的仓库. 在Repository name填入learngit,其他保持默认设置,点击“Create re ...

  5. php excel文件导出之二 图像导出

    PHP文件导出 之图像 和 文字同一时候导出 事实上之前写了个php文件导出.跟这个极为相似,由于项目须要对图像进行导出.查询一番.又写了一个, 这个能实现图像的导出(仅仅能是本地图像,不能使用远程图 ...

  6. mapreduce_template

    Hadoop Tutorial - YDN https://developer.yahoo.com/hadoop/tutorial/module4.html import java.io.IOExce ...

  7. VueJS计算属性: computed

    computed属性 HTML <!DOCTYPE html> <html> <head> <meta charset="utf-8"&g ...

  8. Lua学习七----------Lua函数

    © 版权声明:本文为博主原创文章,转载请注明出处 1.Lua函数 - 完成指定的任务,这种情况下函数作为调用语句使用 - 计算并返回值,这种情况下函数作为赋值语句的表达式使用 - Lua函数可以返回多 ...

  9. Struts2学习一----------Struts2的工作原理及HelloWorld简单实现

    © 版权声明:本文为博主原创文章,转载请注明出处 Struts2工作原理 一个请求在Struts2框架中的处理步骤: 1.客户端初始化一个指向Servlet容器(例如Tomcat)的请求 2.这个请求 ...

  10. python opener代理

    链接:http://www.jb51.net/article/46495.htm https://www.cnblogs.com/cunyusup/p/7341829.html