hdu 3247 AC自动+状压dp+bfs处理

Resource Archiver

Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 100000/100000 K (Java/Others)
Total Submission(s): 2382    Accepted Submission(s): 750

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

/*
hdu 3247 AC自动+状压dp+bfs处理

给你n个正常子串,m个病毒子串,求出最短的字符串(包含所有正常子串,不包含病毒串)

因为正常子串只有十个,所以考虑二进制来记录。
即dp[i][j]表示 包含的正常串的状态为i,最后一步的状态为j的最短情况.
然后试了下发现超内存 卒~

然后膜拜大神,发现我们可以预处理出来正常串之间的最短距离. 像这样我们只需要枚举所有的
正常串. 而我原先那个思路需要枚举所有的节点总共需要dp[1<<10][60040].  而对于通过bfs优化后
我们只需要枚举正常串 最多有11个 ->  dp[1<<10][11].

hhh-2016-04-30 14:34:51
*/
#include <iostream>
#include <vector>
#include <cstring>
#include <string>
#include <cstdio>
#include <queue>
#include <algorithm>
#include <functional>
#include <map>
using namespace std;
#define lson  (i<<1)
#define rson  ((i<<1)|1)
typedef unsigned long long ll;
typedef unsigned int ul;
const int mod = 20090717;
const int INF = 0x3f3f3f3f;
const int N = 100050;
int cnt;
int n,m;
int dp[1<<10][205];
int G[205][205];
int pos[205];
struct Tire
{
    int nex[N][2],fail[N],ed[N];

    int root,L;
    int newnode()
    {
        for(int i = 0; i < 2; i++)
            nex[L][i] = -1;
        ed[L++] = 0;
        return L-1;
    }

    void ini()
    {
        L = 0,root = newnode();
    }

    void inser(char buf[],int val)
    {
        int len = strlen(buf);
        int now = root;
        for(int i = 0; i < len; i++)
        {
            int ta = buf[i]-‘0‘;
            if(nex[now][ta] == -1)
                nex[now][ta] = newnode();
            now = nex[now][ta];
        }
        if(val < 0)
        ed[now] = val;
        else
            ed[now] = (1<<val);
    }

    void build()
    {
        queue<int >q;
        fail[root] = root;
        for(int i = 0; i < 2; i++)
            if(nex[root][i] == -1)
                nex[root][i] = root;
            else
            {
                fail[nex[root][i]] = root;
                q.push(nex[root][i]);
            }
        while(!q.empty())
        {
            int now = q.front();
            q.pop();
            if(ed[fail[now]] < 0)
                ed[now] = ed[fail[now]];
            else if(ed[now] == 0)
                ed[now] = ed[fail[now]];
            for(int i = 0; i < 2; i++)
            {
                if(nex[now][i] == -1)
                    nex[now][i] = nex[fail[now]][i];
                else
                {
                    fail[nex[now][i]] = nex[fail[now]][i];
                    q.push(nex[now][i]);
                }
            }
        }
    }
    int dis[N];
    void Path(int k)
    {
        int now;
        queue<int >q;
        q.push(pos[k]);
        memset(dis,-1,sizeof(dis));
        dis[pos[k]] = 0;
        while(!q.empty())
        {
            now = q.front();
            q.pop();
            for(int i =0;i < 2;i++)
            {
                int t = nex[now][i];
                if(dis[t] < 0 && ed[t] >= 0)
                {
                    dis[t] = dis[now]+1;
                    q.push(t);
                }
            }
        }
        for(int i = 0;i < cnt;i++)
        {
            G[k][i] = dis[pos[i]];
        }
    }

    int Min(int a,int b)
    {
        if(a < 0)
            return b;
        else if(b < 0)
            return a;
        else
            return min(a,b);
    }

    void solve()
    {
        memset(dp,-1,sizeof(dp));
        dp[0][0] = 0;

        for(int i = 0;i < (1<<n);i++)
        {
            for(int j = 0;j < cnt;j++)
            {
                if(dp[i][j] < 0)
                    continue;
                for(int k = 0;k < cnt;k++)
                {
                    if(G[j][k] < 0)
                        continue;
                    int t = (i|ed[pos[k]]);
                    dp[t][k] = Min(dp[i][j] + G[j][k],dp[t][k]);
                }
            }
        }
        int ans = -1;
        for(int i = 0;i < cnt;i++)
        {
            ans = Min(ans,dp[(1<<n)-1][i]);
        }
        cout << ans <<"\n";
    }
};

Tire ac;
char buf[N];
int main()
{
    while(scanf("%d%d",&n,&m)==2 && n && m)
    {
        ac.ini();
        for(int i = 0; i < n; i++)
        {
            scanf("%s",buf);
            ac.inser(buf,i);
        }
        for(int i =0 ; i < m; i++)
        {
            scanf("%s",buf);
            ac.inser(buf,-1);
        }
        ac.build();
        pos[0] = 0;
        cnt = 1;
        for(int i = 0; i < ac.L; i++)
        {
            if(ac.ed[i] > 0)
                pos[cnt++] = i;
        }
        memset(G,-1,sizeof(G));
        for(int i = 0; i < cnt; i++)
            ac.Path(i);
        ac.solve();
    }
    return 0;
}

  

时间: 2024-12-23 19:53:12

hdu 3247 AC自动+状压dp+bfs处理的相关文章

hdu 2825 aC自动机+状压dp

Wireless Password Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5640    Accepted Submission(s): 1785 Problem Description Liyuan lives in a old apartment. One day, he suddenly found that there

HDU 3341 Lost&#39;s revenge(AC自动机+状压DP)

Lost's revenge Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 4548    Accepted Submission(s): 1274 Problem Description Lost and AekdyCoin are friends. They always play "number game"(A bor

HDU 4924 Football Manager(状压DP)

题目连接 : http://acm.hdu.edu.cn/showproblem.php?pid=4924 题意 : n(<=20)个人选出11个人作为首发组成a-b-c阵容,每个人都有自己擅长的位置,并且每个人和其他人会有单向的厌恶和喜欢关系,每个人对于自己擅长的位置都有两个值CA和PA,有喜欢或者厌恶关系的两个人在一起也会影响整个首发的CA总值,要求选出一套阵容使得CA最大,或者CA一样的情况下PA最大. 思路 : 状压搞,dp[s]s的二进制表示20个人中选了那几个人,然后规定选进来的顺序

poj 1699 Best Sequence(AC自动机+状压DP)

题目链接:poj 1699 Best Sequence 题目大意:给定N个DNA序列,问说最少多长的字符串包含所有序列. 解题思路:AC自动机+状压DP,先对字符串构造AC自动机,然后在dp[s][i]表示匹配了s,移动到节点i时候的最短步数. #include <cstdio> #include <cstring> #include <queue> #include <vector> #include <iostream> #include &

HDU 1074 Doing Homework(状压DP)

Problem Description Ignatius has just come back school from the 30th ACM/ICPC. Now he has a lot of homework to do. Every teacher gives him a deadline of handing in the homework. If Ignatius hands in the homework after the deadline, the teacher will r

【HDU3341】 Lost&#39;s revenge (AC自动机+状压DP)

Lost's revenge Time Limit: 5000MS Memory Limit: 65535KB 64bit IO Format: %I64d & %I64u Description Lost and AekdyCoin are friends. They always play "number game"(A boring game based on number theory) together. We all know that AekdyCoin is t

HDU-4771 Stealing Harry Potter&#39;s Precious 状压DP+BFS

哈利波特假期回姨夫家的时候会把他的宝贝藏在地精银行,现在要偷他的宝贝,银行的房间分为可破坏与不可破坏两种,其实就是可到达与不可到达,然后给出哈利的k个宝贝放的位置,如果能全部拿到输出最小的步数,不能拿到则输出-1,用BFS搜索,最先搜到的肯定就是步数最小的,搜不到则输出-1.最近做的好多DP题都跟搜索有关系,看来还是多方面都得会才行啊. #include <iostream> #include <cstdio> #include <cmath> #include <

HDU 5765 Bonds(状压DP)

[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5765 [题目大意] 给出一张图,求每条边在所有边割集中出现的次数. [题解] 利用状压DP,计算不同的连通块,对于每条边,求出两边的联通块的划分方案数,就是对于该点的答案. [代码] #include <cstdio> #include <algorithm> #include <cstring> using namespace std; int n,m,T,Cas=1

HDU 3001 Travelling (状压DP,3进制)

题意: 给出n<=10个点,有m条边的无向图.问:可以从任意点出发,至多经过同一个点2次,遍历所有点的最小费用? 思路: 本题就是要卡你的内存,由于至多可经过同一个点2次,所以只能用3进制来表示,3进制可以先将表打出来.在走的时候注意只能走2次,其他的和普通的TSP状压DP是一样的.注意点:重边,自环等等,老梗了. 1 //#include <bits/stdc++.h> 2 #include <iostream> 3 #include <cstdio> 4 #i