Ring HDU - 2296 AC自动机+简单DP和恶心的方案输出

时间:2022-05-08 22:52:20

题意:

就是现在给出m个串,每个串都有一个权值,现在你要找到一个长度不超过n的字符串,

其中之前的m个串每出现一次就算一次那个字符串的权值,

求能找到的最大权值的字符串,如果存在多个解,输出最短的字典序最小的串。

当最大全权值为0时输出空串。

输入最多100个子串,权值为不超过100的正整数。

每个子串长度至少为1,不超过10, n <= 50

如果不考虑方案输出,这题就变得相当简单了。

dp【i】【j】表示走到长度为 i 的时候 ,到AC自动机 j 这个节点所获得的最大权值和。

我一开始的做法是在dp的过程中获得那个最优方案。

最后死活过不去,就换了一种超级暴力的写法。

将所有情况保存下来,去一个个找字典序最小的方案。

 #include <set>
#include <map>
#include <stack>
#include <queue>
#include <cmath>
#include <cstdio>
#include <string>
#include <vector>
#include <time.h>
#include <cstring>
#include <iostream>
#include <algorithm> #define pi acos(-1.0)
#define eps 1e-9
#define fi first
#define se second
#define rtl rt<<1
#define rtr rt<<1|1
#define bug printf("******\n")
#define mem(a, b) memset(a,b,sizeof(a))
#define name2str(x) #x
#define fuck(x) cout<<#x" = "<<x<<endl
#define sf(n) scanf("%d", &n)
#define sff(a, b) scanf("%d %d", &a, &b)
#define sfff(a, b, c) scanf("%d %d %d", &a, &b, &c)
#define sffff(a, b, c, d) scanf("%d %d %d %d", &a, &b, &c, &d)
#define pf printf
#define FIN freopen("../date.txt","r",stdin)
#define gcd(a, b) __gcd(a,b)
#define lowbit(x) x&-x
#define IO iOS::sync_with_stdio(false) using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const int maxn = 1e6 + ;
const int maxm = 8e6 + ;
const int INF = 0x3f3f3f3f;
const int mod = ; char str[][];
int n, m, cost[]; struct Aho_Corasick {
int next[][], fail[], End[];
int root, cnt; int newnode() {
for (int i = ; i < ; i++) next[cnt][i] = -;
End[cnt++] = ;
return cnt - ;
} void init() {
cnt = ;
root = newnode();
} void insert(char buf[], int id) {
int len = strlen(buf);
int now = root;
for (int i = ; i < len; i++) {
if (next[now][buf[i] - 'a'] == -) next[now][buf[i] - 'a'] = newnode();
now = next[now][buf[i] - 'a'];
}
End[now] += id;
} void build() {
queue<int> Q;
fail[root] = root;
for (int i = ; i < ; 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]];
for (int i = ; i < ; 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 debug() {
for (int i = ; i < cnt; i++) {
printf("id = %3d,fail = %3d,end = %3d,chi = [", i, fail[i], End[i]);
for (int j = ; j < ; j++) printf("%2d", next[i][j]);
printf("]\n");
}
}
} ac; int dp[][];
string path[][]; int main() {
// FIN;
int T;
sf(T);
while (T--) {
sff(n, m);
for (int i = ; i < m; ++i) scanf("%s", str[i]);
for (int i = ; i < m; i++) sf(cost[i]);
ac.init();
for (int i = ; i < m; ++i) ac.insert(str[i], cost[i]);
ac.build();
mem(dp, -);
for (int i = ; i <= n; i++)
for (int j = ; j < ac.cnt; j++)
path[i][j] = "";
int ans = , num1 = , num2 = ;
dp[][] = ;
for (int i = ; i < n; ++i) {
for (int j = ; j < ac.cnt; ++j) {
if (dp[i][j] == -) continue;
for (int k = ; k < ; ++k) {
int idx = ac.next[j][k];
if (dp[i + ][idx] < dp[i][j] + ac.End[idx]) {
dp[i + ][idx] = dp[i][j] + ac.End[idx];
path[i + ][idx] = path[i][j] + char(k + 'a');
} else if (dp[i + ][idx] == dp[i][j] + ac.End[idx] &&
path[i + ][idx] > path[i][j] + char(k + 'a')) {
//fuck(path[i][j]);
path[i + ][idx] = path[i][j] + char(k + 'a');
// fuck(path[i][j]); }
}
}
}
for (int i = ; i <= n; i++) {
for (int j = ; j < ac.cnt; ++j) {
if (ans < dp[i][j]) {
ans = dp[i][j], num1 = i, num2 = j;
} else if (ans == dp[i][j]&&path[num1][num2] > path[i][j] && path[num1][num2].size()>=path[i][j].size()) {
num1 = i, num2 = j;
}
}
}
// printf("%d\n", ans);
cout << path[num1][num2] << endl;
}
return ;
}