\(\quad\) Great! Your new software is almost finished! The only thing left to do is archiving all your n resource files into a big one.\(\quad\) Wait a minute… you realized that it isn’t as easy as you thought. Think about the virus killers. They’ll find your software suspicious, if your software contains one of the m predefined virus codes. You absolutely don’t want this to happen.\(\quad\) Technically, resource files and virus codes are merely 01 strings. You’ve already convinced yourself that none of the resource strings contain a virus code, but if you make the archive arbitrarily, virus codes can still be found somewhere.\(\quad\) Here comes your task (formally): design a 01 string that contains all your resources (their occurrences can overlap), but none of the virus codes. To make your software smaller in size, the string should be as short as possible.
\(\quad\) There will be at most 10 test cases, each begins with two integers in a single line: n and m (2 <= n <= 10, 1 <= m <= 1000). The next n lines contain the resources, one in each line. The next m lines contain the virus codes, one in each line. The resources and virus codes are all non-empty 01 strings without spaces inside. Each resource is at most 1000 characters long. The total length of all virus codes is at most 50000. The input ends with n = m = 0.
Output \(\quad\) For each test case, print the length of shortest string.
2 2 1110 0111 101 1001 0 0
Sample Output 5
題意 \(\quad\) 就是給你\(n\) 個需要的串和\(m\) 個病毒串,最后讓你構(gòu)造一個字符串,包含所有需要的串,不包括任何病毒串。
思路 \(\quad\) 先講\(n+m\) 個串全部插入ac自動機中,然后去構(gòu)造\(fail\) 指針的時候注意將\(fail\) 節(jié)點的信息傳遞給子節(jié)點。\(\quad\) 首先容易想到\(dp[i][j]\) 表示ac自動機上狀態(tài)為\(i\) ,包含需要串的狀態(tài)為\(j\) 時所需要的最少字符串數(shù)。但是數(shù)據(jù)范圍\(i<=(略大于)5e4,j<=1024\) ,會\(MLE\) 。\(\quad\) 觀察數(shù)據(jù)范圍可以發(fā)現(xiàn)\(n<<m\) ,那么說明,在ac自動機上,無用的節(jié)點占大多數(shù),那么就可以找出全部有用的節(jié)點,用\(bfs\) 求出所有有用節(jié)點兩兩之間的最小距離,然后直接在這些有用的點上跑\(dp\) 就可以了,那么可以得到新的\(dp\) 方程。\(\quad\) \(dp[i][j]\) 表示到有用節(jié)點\(i\) ,包含需要串的狀態(tài)為\(j\) 時所需要的最少字符長度。\(\quad\) \(cnt[i]\) 表示有用節(jié)點\(i\) 上包含需要串的狀態(tài)。\(\quad\) \(cc[i][j]\) 表示從有用節(jié)點\(i\) 走到有用節(jié)點\(j\) 需要的最少字符數(shù)。\(\quad\) \(dp[k][j|cnt[k]] = min(dp[k][cnt[k]],dp[i][j]+cc[i][k])\) 。\(\quad\) 最后在遍歷一遍\(dp[i][mx]\) ,就可以得到答案。
/***************************************************************> File Name : a.cpp> Author : Jiaaaaaaaqi> Created Time : 2019年04月29日 星期一 11時10分00秒***************************************************************/#include <map>
#include <set>
#include <list>
#include <ctime>
#include <cmath>
#include <stack>
#include <queue>
#include <cfloat>
#include <string>
#include <vector>
#include <cstdio>
#include <bitset>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define lowbit(x) x & (-x)
#define mes(a, b) memset(a, b, sizeof a)
#define fi first
#define se second
#define pii pair<int, int>typedef unsigned long long int ull;
typedef long long int ll;
const int maxn = 6e4 + 10;
const int maxm = 1e5 + 10;
const ll mod = 1e9 + 7;
const ll INF = 1e18 + 100;
const int inf = 0x3f3f3f3f;
const double pi = acos(-1.0);
const double eps = 1e-8;
using namespace std;int n, m;
int cas, tol, T;char s[1010];
struct AC {int node[maxn][2], fail[maxn], cnt[maxn], vir[maxn];int root, sz;int newnode() {mes(node[++sz], 0);cnt[sz] = vir[sz] = 0;return sz;}void init() {sz = 0;root = newnode();}void insert(char *s, int f, int id) {int len = strlen(s+1);int rt = root;for(int i=1; i<=len; i++) {int k = s[i]-'0';if(node[rt][k] == 0) {node[rt][k] = newnode();}rt = node[rt][k];}if(f == 1) cnt[rt] |= (1<<(id-1));else vir[rt] = 1;}void build() {queue<int> q;while(!q.empty()) q.pop();fail[root] = root;for(int i=0; i<=1; i++) {if(node[root][i] == 0) {node[root][i] = root;} else {fail[node[root][i]] = root;q.push(node[root][i]);}}while(!q.empty()) {int u = q.front();q.pop();vir[u] |= vir[fail[u]];cnt[u] |= cnt[fail[u]];for(int i=0; i<=1; i++) {if(node[u][i] == 0) {node[u][i] = node[fail[u]][i];} else {fail[node[u][i]] = node[fail[u]][i];q.push(node[u][i]);}}}}int dis[maxn], point[500];bool vis[maxn];int cc[500][500], dp[500][1030];void bfs(int st) {queue<int> q;while(!q.empty()) q.pop();for(int i=1; i<=sz; i++) dis[i] = inf;mes(vis, 0);q.push(point[st]);dis[point[st]] = 0;vis[point[st]] = 1;while(!q.empty()) {int u = q.front();q.pop();for(int i=0; i<=1; i++) {int k = node[u][i];if(vir[k]) continue;if(vis[k]) continue;dis[k] = dis[u]+1;vis[k] = true;q.push(k);}}for(int i=1; i<=tol; i++) {cc[st][i] = dis[point[i]];}}void handle() {tol = 0;point[++tol] = 1;for(int i=1; i<=sz; i++) {if(cnt[i]) {point[++tol] = i;}}for(int i=1; i<=tol; i++) {bfs(i);}// for(int i=1; i<=tol; i++) {// for(int j=1; j<=tol; j++) {// printf("%d%c", cc[i][j], j==tol ? '\n' : ' ');// }// }// for(int i=1; i<=tol; i++) {// printf("cnt[%d] = %d\n", i, cnt[point[i]]);// }}int solve() {int mx = (1<<n)-1;for(int i=1; i<=tol; i++) {for(int j=0; j<=mx; j++) {dp[i][j] = inf;}}dp[1][0] = 0;for(int j=0; j<=mx; j++) {for(int i=1; i<=tol; i++) {if(dp[i][j] == inf) continue;// printf("dp[%d][%d] = %d\n", i, j, dp[i][j]);for(int k=1; k<=tol; k++) {if(i == k) continue;dp[k][j|cnt[point[k]]] = min(dp[k][j|cnt[point[k]]], dp[i][j]+cc[i][k]);}}}int ans = inf;for(int i=1; i<=tol; i++) {ans = min(ans, dp[i][mx]);}return ans;}
} ac;int main() {while(scanf("%d%d", &n, &m), n||m) {ac.init();for(int i=1; i<=n; i++) {scanf("%s", s+1);ac.insert(s, 1, i);}for(int i=1; i<=m; i++) {scanf("%s", s+1);ac.insert(s, 2, i);}ac.build();ac.handle();int ans = ac.solve();printf("%d\n", ans);}return 0;
}
轉(zhuǎn)載于:https://www.cnblogs.com/Jiaaaaaaaqi/p/10789881.html
創(chuàng)作挑戰(zhàn)賽 新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎
總結(jié)
以上是生活随笔 為你收集整理的HDU - 3247 Resource Archiver (AC自动机,状压dp) 的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔 推薦給好友。