HDU 3247 Resource Archiver

132 篇文章 0 订阅
15 篇文章 0 订阅

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.

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


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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值