1212: [Hnoi2004]l language time limit:10 Sec Memory limit:162 MB
submit:1367 solved:598
[Submit] [Status] [Discuss] 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 ' can be understood under dictionary D because 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
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
For each article entered, you need to output the position of the longest prefix that the article can be understood in dictionary D.
Sample Input4 3
Is
Name
What
Your
Whatisyourname
Whatisyouname
Whaisyourname
Sample Output14
6
0 whole passage ' whatisyourname ' can be understood
The prefix ' Whatis ' can be understood
No prefix can be understood because
for the character I,ac automata to go to X, the corresponding string is a suffix of b[1,i], and all the strings corresponding to the ancestor nodes in the fail tree are a suffix of b[1,i].
so T[u].len represents the length of the word with u ending, so that f[i] is valid for I, enumerating the characters of the article to I when looking for all ancestors on the fail tree (all the legal suffixes of the article [1,i]) then move on. It is said that trie-tree violence can
#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespacestd;Const intn=1e6+5, m=205;intn,m;Chars[n],ss[ the];structnode{intch[ -],fail,fa,len;} T[n];intsz;voidInsChars[]) { intu=0, N=strlen (s+1); for(intI=1; i<=n;i++){ intc=s[i]-'a'; if(!t[u].ch[c]) t[u].ch[c]=++sz; U=T[u].ch[c]; } T[u].len=N;}intQ[n],head,tail;voidLG () {head=tail=1; for(intI=0;i< -; i++) if(t[0].ch[i]) q[tail++]=t[0].ch[i]; while(head!=tail) { intu=q[head++]; for(intI=0;i< -; i++){ int&v=T[u].ch[i]; if(!V) {V=t[t[u].fail].ch[i];Continue;} T[v].fail=T[t[u].fail].ch[i]; Q[tail++]=v; } }}intAns,f[n];voidAC (Chars[]) {ans=0; Memset (F,0,sizeof(f)); f[0]=1; intnow=0, N=strlen (s+1); for(intI=1; i<=n;i++){ intc=s[i]-'a'; now=T[NOW].CH[C];//printf ("Hi%d%d%d\n", i,c,now); for(int_=now;_;_=t[_].fail)if(T[_].len) {F[i]|=f[i-T[_].len]; if(F[i]) {ans=i; Break;} } }}intMain () {//freopen ("In.txt", "R", stdin);scanf"%d%d",&n,&m); for(intI=1; i<=n;i++) scanf ("%s", ss+1), INS (ss); LG (); for(intI=1; i<=m;i++) {scanf ("%s", s+1); AC (s); printf ("%d\n", ans); }}
Bzoj 1212: [hnoi2004]l language [AC automaton DP]