题目链接: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 ;
}

最新文章

  1. [转]Oracle存在则更新,不存在则插入
  2. subversion(SVN)常规使用
  3. Java内存管理:深入Java内存区域
  4. Head First HTML与CSS — 布局与定位
  5. day01:study HTTP协议
  6. python笔记:#007#变量
  7. dataTables 插件学习整理
  8. window.open post
  9. dede织梦后台页面及功能修改精简操作方法
  10. 2018-01-11 Antlr4实现数学四则运算
  11. js数组,字符串,json互相转换函数有哪些
  12. beta6
  13. 2018.08.28 集合堆栈机(模拟+STL)
  14. 实验一 《网络对抗技术》逆向及Bof技术
  15. N!(N的阶乘)最末位非0的求解方法
  16. 简明python教程 --C++程序员的视角(五):面向对象的编程
  17. HDU 2825 Wireless Password【AC自动机+DP】
  18. [BZOJ4872][六省联考2017]分手是祝愿(期望DP)
  19. adore-ng笔记和Linux普通用户提权
  20. java---springMVC与strutsMVC的区别

热门文章

  1. dockerfile VOLUME 对外暴露目录设置问题
  2. Nginx配置文档具体解释
  3. vuex 深入理解
  4. 怎样在编译的时候,控制删除apk不用的资源?
  5. intel电源管理技术中I2C和SVID
  6. sudo apt-get update 没有公钥,无法验证下列签名
  7. HDFS源码分析数据块汇报之损坏数据块检测checkReplicaCorrupt()
  8. hdu 5071 Chat-----2014acm亚洲区域赛鞍山 B题
  9. DataGrid绑定Dictionary问题
  10. ios --转载 在mac上安装让rvm及cocoa pods详解