zoukankan      html  css  js  c++  java
  • HDU 3247 Resource Archiver(AC自动机+状态压缩DP)

    Resource Archiver

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


    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
     
    Source
     
    Recommend
    wujianhua
     
     
     
    AC自动机第二题。。。
    不解释。。。。慢慢体会吧
    /*
    HDU 3247 Resource Archiver
    给你 n 个串,和 m 个病毒串。要把这 n 个串
    连起来来,可以重叠,但不能包含 m 个病毒串
    中的任意一个。求把 n 个串连起来的最小长度。
    
    */
    #include <stdio.h>
    #include <math.h>
    #include <queue>
    #include <string.h>
    #include <algorithm>
    using namespace std;
    const int MAXN=60000;//结点的最大数,50000+1000*10
    const int MAXL=(1<<10);
    const int MAX=2;
    const int MAXM=11;
    typedef struct Trie_Node
    {
        bool virus;//是否病毒
        int end,fail;
        int next[MAX];
    }Trie;
    Trie tree[MAXN];
    char str[50010];
    int dis[MAXN];
    int pos[MAXM];
    int G[MAXM][MAXM];
    int dp[MAXL][MAXM];
    int size,cnt;
    
    
    void insert(char *s,int id)
    {
        int p=0;
        int i=0;
        while(s[i]!='\0')
        {
            if(tree[p].next[s[i]-'0']==0)
            {
                tree[++size].end=0;
                tree[size].fail=0;
                tree[size].virus=false;
                for(int j=0;j<MAX;j++)
                     tree[size].next[j]=0;
                tree[p].next[s[i]-'0']=size;
            }
            p=tree[p].next[s[i]-'0'];
            i++;
        }
        if(id>=0) tree[p].end=(1<<id);
        else tree[p].virus=true;
    }
    void BFS()
    {
        queue<int>q;
        int temp,p;
        q.push(0);
        while(!q.empty())
        {
            temp=q.front();
            q.pop();
            for(int i=0;i<MAX;i++)
            {
                if(tree[temp].next[i]!=0)
                {
                    p=tree[temp].next[i];
                    q.push(p);
                    if(temp!=0)tree[p].fail=tree[tree[temp].fail].next[i];
                    tree[p].end|=tree[tree[p].fail].end;
                    tree[p].virus|=tree[tree[p].fail].virus;
                }
                else tree[temp].next[i]=tree[tree[temp].fail].next[i];
            }
        }
    }
    void Path(int k)
    {
        queue<int>q;
        q.push(pos[k]);
        memset(dis,-1,sizeof(dis));
        dis[pos[k]]=0;
        int now,p;
    
        while(!q.empty())
        {
            now=q.front();
            q.pop();
            for(int i=0;i<MAX;i++)
            {
                p=tree[now].next[i];
                if(dis[p]<0&&!tree[p].virus)
                {
                    dis[p]=dis[now]+1;
                    q.push(p);
                }
            }
        }
        for(int i=0;i<cnt;i++)
            G[k][i]=dis[pos[i]];
    
    }
    inline int MIN(int x,int y)
    {
        if(x<0||y<0)return x>y?x:y;
        else return x>y?y:x;
    }
    void DoIt(int n)
    {
        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|tree[pos[k]].end;
                   dp[t][k]=MIN(dp[t][k],dp[i][j]+G[j][k]);
               }
           }
        int t=(1<<n)-1;
        int ans=-1;
        for(int i=0;i<cnt;i++)
            ans=MIN(ans,dp[t][i]);
        printf("%d\n",ans);
    }
    int main()
    {
        //freopen("in.txt","r",stdin);
        //freopen("out.txt","w",stdout);
        int n,m;
        while(scanf("%d%d",&n,&m))
        {
            if(n==0&&m==0)break;
            tree[0].end=0;
            tree[0].fail=0;
            tree[0].virus=false;
            for(int i=0;i<MAX;i++)tree[0].next[i]=0;
            size=0;
            for(int i=0;i<n;i++)
            {
                scanf("%s",&str);
                insert(str,i);
            }
            for(int i=0;i<m;i++)
            {
                scanf("%s",&str);
                insert(str,-1);
            }
            BFS();
            cnt=1;
            pos[0]=0;
            for(int i=0;i<=size;i++)
              if(tree[i].end)
                 pos[cnt++]=i;
            for(int i=0;i<cnt;i++)
              Path(i);
            DoIt(n);
        }
        return 0;
    }
  • 相关阅读:
    codeforces 466D
    codeforces 360B
    codeforces 383D
    codeforces 679B
    codeforces 571B
    codeforces 494B
    CodeForces 660D Number of Parallelograms(n个点所能组成的最多平行四边形数量)
    算法竞赛模板 最短路
    算法竞赛模板 string中substr函数的运用
    HDOJ 1269 迷宫城堡(tarjan模板题)
  • 原文地址:https://www.cnblogs.com/kuangbin/p/2628572.html
Copyright © 2011-2022 走看看