HDU2296 Ring —— AC自动机 + DP

时间:2023-03-09 17:49:05
HDU2296 Ring —— AC自动机 + DP

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