• HDU-3247 Resource Archiver(AC自动机+BFS)


    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

    题目大意:给n个资源01串和m个病毒01串。构造一个最短的01串使其包含所有的资源串,但不包含任何一个病毒串。
    题目分析:建立好AC自动机后,在上面广搜即可。

    代码如下:
    # include<iostream>
    # include<cstdio>
    # include<queue>
    # include<cstring>
    # include<algorithm>
    using namespace std;
    
    const int N=10000;
    
    int ch[6*N+5][2];
    int fail[6*N+5];
    int sz,type[6*N+5];
    
    void init()
    {
    	sz=0;
    	memset(ch,-1,sizeof(ch));
    	memset(type,0,sizeof(type));
    }
    
    int idx(char c)
    {
    	return c-'0';
    }
    
    void insert(char *s,int val)
    {
    	int r=0;
    	int n=strlen(s);
    	for(int i=0;i<n;++i){
    		int c=idx(s[i]);
    		if(ch[r][c]==-1) ch[r][c]=++sz;
    		r=ch[r][c];
    	}
    	type[r]=val;
    }
    
    void getFail()
    {
    	queue<int>q;
    	fail[0]=0;
    	for(int i=0;i<2;++i){
    		if(ch[0][i]==-1)
    			ch[0][i]=0;
    		else{
    			q.push(ch[0][i]);
    			fail[ch[0][i]]=0;
    		}
    	}
    	while(!q.empty())
    	{
    		int u=q.front();
    		q.pop();
    		if(type[fail[u]]>0)
    			type[u]|=type[fail[u]];
    		for(int i=0;i<2;++i){
    			if(ch[u][i]==-1)
    				ch[u][i]=ch[fail[u]][i];
    			else{
    				fail[ch[u][i]]=ch[fail[u]][i];
    				q.push(ch[u][i]);
    			}
    		}
    	}
    }
    
    struct Node
    {
    	int step;
    	int loca;
    	int sta;
    };
    int n,m;
    char s[1005];
    char vis[6*N+5][1<<10];
    
    int bfs()
    {
    	queue<Node>q;
    	memset(vis,0,sizeof(vis));
    	q.push(Node{0,0,0});
    	while(!q.empty())
    	{
    		Node u=q.front();
    		q.pop();
    		if(u.sta==(1<<n)-1)
    			return u.step;
    		for(int i=0;i<2;++i) if(type[ch[u.loca][i]]>=0){
    			int v=ch[u.loca][i];
    			if(vis[v][u.sta|type[v]]) continue;
    			vis[v][u.sta|type[v]]=1;
    			q.push(Node{u.step+1,v,u.sta|type[v]});
    		}
    	}
    	return -1;
    }
    
    int main()
    {
    	while(~scanf("%d%d",&n,&m)&&(n+m))
    	{
    		init();
    		for(int i=0;i<n;++i){
    			scanf("%s",s);
    			insert(s,1<<i);
    		}
    		for(int i=0;i<m;++i){
    			scanf("%s",s);
    			insert(s,-1);
    		}
    		getFail();
    		printf("%d
    ",bfs());
    	}
    	return 0;
    }
    

      


  • 相关阅读:
    C#(99):C# 6.0 新特性(.NET Framework 4.6 与 Visual Studio 2015 )
    C#(99):C# 3.0 新特性2:( NET Framework 3.5 与 Visual Studio 2008 )隐式类型、对象、集合初始值设定项、匿名类型、匿名对象
    C#(99):C# 3.0 新特性1:( NET Framework 3.5 与 Visual Studio 2008 )自动实现属性、扩展方法、Lambda,Linq,表达式树
    Scrapy爬虫框架实战案例(适合小白人门)
    惊!Python居然可以读故事了(附源码)
    识别豆瓣登录滑动验证码(附源码)
    「找一找」考你眼力的时候到了!
    想知道「双十一」淘宝商家销售数据?快来看看!!!
    人工智能下的音频还能这样玩!!!!
    想增加你的词汇量吗?---教你如何爬取某贝单词
  • 原文地址:https://www.cnblogs.com/20143605--pcx/p/6021157.html
Copyright © 2020-2023  润新知