HDU 3247 Resource Archiver
来源:互联网 发布:个人陈述 知乎 编辑:程序博客网 时间:2024/06/07 07:43
Problem Description
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.
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 21110011110110010 0
Sample Output
5
ac自动机处理合并费用,状态压缩dp得出答案。
#include<cstdio>#include<algorithm>#include<cstring>#include<queue>using namespace std;const int size = 2;const char base = '0';const int maxn = 1005;int n, m;class tire{public: tire *next[size], *fail; int cnt, virus, deep, id; tire(){ cnt = -1; id = virus = 0; fail = NULL; memset(next, 0, sizeof(next)); }};class ac{private: tire *root, *node[15]; int map[15][15], dp[1 << 11][11], tot,vis[60005]; char s[maxn];public: ac(){ memset(map, 0, sizeof(map)); root = new tire; tot = 0; } void clear(){ memset(map, 0, sizeof(map)); root = new tire; tot = 0; } void insert(int x) { scanf("%s", s); tire *j = root; for (int i = 0, k; s[i]; i++) { k = s[i] - base; if (!j->next[k]) { j->next[k] = new tire; j->next[k]->id = ++tot; } j = j->next[k]; } if (x < 0) j->virus = 1; else { node[x] = j; j->cnt = x; map[x][x] = strlen(s); } } void getfail() { root->fail = root; queue<tire *> p; for (int i = 0; i < size; i++) { if (root->next[i]) { root->next[i]->fail = root; p.push(root->next[i]); } else root->next[i] = root; } tire *q, *k; while (!p.empty()) { q = p.front(); p.pop(); k = q->fail; for (int i = 0; i < size; i++) if (q->next[i]) { q->next[i]->fail = k->next[i]; p.push(q->next[i]); } else q->next[i] = k->next[i]; } } void getmap() { queue<tire *> p; tire *q, *k; for (int i = 0; i < n; i++) { p.push(node[i]); node[i]->deep = 0; memset(vis, 0, sizeof(vis)); vis[node[i]->id] = 1; while (!p.empty()) { q = p.front(); p.pop(); for (int j = 0; j < size; j++) if (!vis[q->next[j]->id]) { k = q->next[j]; k->deep = q->deep + 1; if (k->cnt >= 0 && !map[i][k->cnt]) map[i][k->cnt] = k->deep; if (!k->virus){ p.push(k); vis[k->id] = 1; } } } } } int work_out() { getfail(); getmap(); memset(dp, 1, sizeof(dp)); for (int i = 0; i < n; i++) dp[1 << i][i] = map[i][i]; for (int i = 1; i < (1 << n); i++) { for (int j = 0; j < n; j++) if (i&(1 << j)) for (int k = 0; k < n;k++) if ((i&(1 << k))==0) if (map[j][k]) { dp[i | (1 << k)][k] = min(dp[i | (1 << k)][k], dp[i][j] + map[j][k]); } } int ans = 0x7FFFFFFF; for (int i = 0; i < n; i++) ans = min(ans, dp[(1 << n) - 1][i]); return ans; }}f;int main(){ while (scanf("%d%d", &n, &m), n + m) { f.clear(); for (int i = 0; i < n; i++) f.insert(i); while (m--) f.insert(-1); printf("%d\n", f.work_out()); }}
0 0
- hdu 3247 Resource Archiver
- hdu 3247 Resource Archiver
- HDU 3247 Resource Archiver
- HDU 3247Resource Archiver
- HDU 3247 Resource Archiver(AC自动机+BFS+状态DP)
- hdu 3247 Resource Archiver(AC自动机+状压DP)
- [AC自动机+spfa+状压dp] hdu 3247 Resource Archiver
- hdu 3247 Resource Archiver(AC自动机+BFS+DP)
- hdu 3247 Resource Archiver (ac自动机+BFS+状压dp)
- HDU 3247 Resource Archiver[AC自动机+最短路+dp]
- HDU 3247 Resource Archiver(AC自动机+BFS+状态DP)
- HDU 3247-Resource Archiver(AC自动机+BFS+DP)
- HDU 3247 Resource Archiver AC自动机 + BFS最短路 + 状压DP
- HDU 3247 Resource Archiver (AC自动机 + BFS + 状态压缩DP)
- 【AC自动机】 HDOJ 3247 Resource Archiver
- Resource Archiver hdu3247
- HDU3247 Resource Archiver
- HDU3247 Resource Archiver(AC自动机+DP)
- linux下socket编程实例
- HDU 3341 Lost's revenge
- 我的第一个svm程序:手写字识别
- DAO设计模式
- 单链表的基本操作
- HDU 3247 Resource Archiver
- C++中随机函数rand()和srand()的用法
- 第三章:UVa227 解题报告
- poj 杂题 - 1083 Moving Tables
- mixin
- 特殊数据类型成员变量的初始化(转)
- Shell基础-Bash变量-环境变量(上)
- Objective-C中设计模式总结
- HDU--1698(线段树,区间修改成段替换)