HDU 3247 Resource Archiver AC自動機+BFS+dp
Resource Archiver
Great! Your new software is almost finished! The only thing left to do is archiving all your n resource files into a big one.
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.
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.
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.
Input
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
For each test case, print the length of shortest string.
Sample Input
2 2 1110 0111 101 1001 0 0
Sample Output
5
題解:很容易想到狀壓dp,dp[i][j],表示目前所含資源編碼狀態i且以自動機狀態j為結尾的最小長度,那麼可以知道所接下一個編碼所新增的最小長度,p[i][j]表示第i個編碼後接第j個編碼的最小長度,可以bfs求出,然後就是一個普通的dp了。
#include<stdio.h> #include<algorithm> #include<string.h> #include<queue> using namespace std; #define ll long long const int mod = 100000; const int maxm = 600006; const int INF = 1e9 + 7; int n, cnt; int tr[maxm][2], fail[maxm], last[maxm], pos[maxm], dis[maxm], p[15][15], dp[1025][15]; char str[maxm], ch[maxm]; void init() { memset(last, 0, sizeof(last)); memset(tr, 0, sizeof(tr)); memset(fail, 0, sizeof(fail)); cnt = 0; } int insert(int flag) { int len = strlen(str); int now = 0; for (int i = 0;i < len;i++) { int num = str[i] - '0'; if (!tr[now][num]) tr[now][num] = ++cnt; now = tr[now][num]; } last[now] = flag; return now; } void find_fail() { int now; queue<int>q; for (int i = 0;i < 2;i++) if (tr[0][i]) q.push(tr[0][i]); while (!q.empty()) { now = q.front();q.pop(); if (last[fail[now]] == -1) last[now] = -1; else last[now] |= last[fail[now]]; for (int i = 0;i < 2;i++) { if (tr[now][i]) { fail[tr[now][i]] = tr[fail[now]][i]; q.push(tr[now][i]); } else tr[now][i] = tr[fail[now]][i]; } } } void bfs(int s, int L) { memset(dis, -1, sizeof(dis)); dis[pos[s]] = 0; queue<int>q; q.push(pos[s]); while (!q.empty()) { int u = q.front();q.pop(); for (int i = 0;i < 2;i++) { int v = tr[u][i]; if (dis[v] == -1 && last[v] != -1) { dis[v] = dis[u] + 1; q.push(v); } } } for (int i = 0;i <= L;i++) p[s][i] = dis[pos[i]]; } int main() { int i, j, k, n, m, sum, ans; while (scanf("%d%d", &n, &m), n || m) { init(); for (i = 1;i <= n;i++) { scanf("%s", str); insert(1 << (i - 1)); } for (i = 1;i <= m;i++) { scanf("%s", str); insert(-1); } find_fail(); sum = 0; for (i = 0;i <= cnt;i++) if (last[i] > 0) pos[++sum] = i; bfs(0, sum); for (i = 1;i <= sum;i++) bfs(i, sum); memset(dp, INF, sizeof(dp)); dp[0][0] = 0; for (i = 0;i < (1 << n);i++) { for (j = 0;j <= sum;j++) { if (dp[i][j] == INF) continue; for (k = 0;k <= sum;k++) { if (j == k || p[j][k] == -1) continue; int v = i | last[pos[k]]; dp[v][k] = min(dp[v][k], dp[i][j] + p[j][k]); } } } ans = INF; for (i = 1;i <= sum;i++) ans = min(ans, dp[(1 << n) - 1][i]); printf("%lld\n", ans); } return 0; }