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;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值