HDU-3247 Resource archiver (ac automaton +bfs)

Source: Internet
Author: User

Description

great! Your new software is almost finished! The only thing left and do are archiving all your n resource files into a big one.
Wait a minute ... you realized the 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 strings. You've already convinced yourself that's none of the resource strings contain a virus code, but if you make the archive Arbi trarily, virus codes can still be found somewhere.
Here Comes your task (formally): Design A in string that contains all your resources (their occurrences can overlap), but None of the virus codes. Your software smaller in size, the string should is as short as possible.

Input

There'll is at the most test cases, each begins with the integers in a single line:n and M (2 <= n <=, 1 <= M <= 1000). The next n lines contain the resources, one in each line. The next m lines contain the virus codes. The resources and virus codes is all non-empty strings without spaces inside. Each resource are at the most characters long. The total length of all virus codes are at most 50000. The input ends with n = m = 0.

Output

For each test case, print the length of shortest string.

Sample Input

2 21110011110110010 0

Sample Output

 5 

Topic: Give N Resources 01 strings and M virus 01 strings. Constructs a shortest 01 string that contains all the resource strings, but does not contain any of the virus strings.
Problem Analysis: After the establishment of a good AC automatic machine, on the above wide search can be. The

code is as follows:
# 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 ();p rintf ("%d\n", BFS ());} return 0;}

  

HDU-3247 Resource archiver (ac automaton +bfs)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.