Title Description
The appearance of punctuation is later than the appearance of text, so the previous language is no punctuation. Now all you have to deal with is an article with no punctuation.
A section of article T is made up of several lowercase letters. A word w is also composed of several lowercase letters. A Dictionary of D is a collection of several words. We call an article T under a dictionary D to be understood, that is, if the article T can be divided into several parts, and each part is a word in dictionary d.
For example dictionary D includes the word {' is ', ' name ', ' What ', ' your '}, then the article ' Whatisyourname ' is understood under dictionary D, as it can be divided into 4 words: ' What ', ' is ', ' your ', ' Name ', and each word belongs to dictionary d, and the article ' Whatisyouname ' cannot be understood under dictionary D, but can be understood under dictionary d ' =d+{' you '. A prefix ' whatis ' of this passage can also be understood under dictionary D, and is the longest prefix that can be understood under dictionary D.
Given a dictionary d, your program needs to determine whether several passages can be understood under dictionary D. and gives the position of the longest prefix that can be understood under dictionary D.
Input/output format
Input format:
The first line of the input file is two positive integers n and m, which indicates that there are n words in dictionary d, and that there is an M segment that needs to be processed. The next n lines describe one word per line, and then each row of M lines describes an article.
Among them 1<=n, m<=20, each word length does not exceed 10, each paragraph length does not exceed 1M.
Output format:
For each article entered, you need to output the position of the longest prefix that the article can be understood in dictionary D.
Input and Output sample input sample #: Copy
4 3 Isnamewhatyourwhatisyournamewhatisyounamewhaisyourname
Output Example # #: Replication
( entire article ' Whatisyourname ' can be understood) 6 (prefix ' whatis ' can be understood) 0 (no prefix can be understood)
F[i] Indicates whether the string ending with the end of I can be understood
F[I]|=F[J] (j+1~i bit is a word)
Build an AC automaton to save the word length
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <algorithm>5#include <cmath>6#include <queue>7 using namespacestd;8 intsize,ch[2001][ -],val[2001],fail[2001],ans;9 intf[1000005];Ten Chars[1000005]; Onequeue<int>Q; A voidInsertintlen) -{inti; - intnow=0; the for(i=0; i<len;i++) - { - if(ch[now][s[i]-'a']==0) ch[now][s[i]-'a']=++size; -now=ch[now][s[i]-'a']; + } -val[now]=Len; + } A voidAc_build () at{inti; - for(i=0;i< -; i++) - if(ch[0][i]) fail[ch[0][i]]=0, Q.push (ch[0][i]); - while(Q.empty () = =0) - { - intu=Q.front (); in Q.pop (); - for(i=0;i< -; i++) to { + if(Ch[u][i]) fail[ch[u][i]]=Ch[fail[u]][i],q.push (Ch[u][i]); - Elsech[u][i]=Ch[fail[u]][i]; the } * } $ }Panax Notoginseng voidSolve () -{intI,j,len; the intnow=0; +scanf"%s", s); Alen=strlen (s); thef[0]=1; + for(i=1; i<=len;i++) - { $now=ch[now][s[i-1]-'a']; $ for(j=now;j;j=Fail[j]) - { - if(i-val[j]>=0) thef[i]|=f[i-Val[j]]; - if(F[i]) Break;Wuyi } the } - for(i=0; i<=len;i++) Wu { - if(F[i]) ans=i; Aboutf[i]=0; $ } -printf"%d\n", ans); - } - intMain () A{intN,m,i,len; +Cin>>n>>m; the for(i=1; i<=n;i++) - { $scanf"%s", s); thelen=strlen (s); the Insert (len); the } the ac_build (); - for(i=1; i<=m;i++) in { the solve (); the } About}
[HNOI2004] L language