1212: [Hnoi2004]l language

Source: Internet
Author: User

1212: [Hnoi2004]l language time limit:10 Sec Memory limit:162 MB
submit:643 solved:252
[Submit] [Status] 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

HINT Source

Dp string

The problem: I really do not understand why this question is the last DP label ... Feel Good dizzy ... Say the idea--first based on the dictionary to establish an AC automaton, and then use this to run, run, run, run ... Use an array to record whether the prefix of length n can be accepted, then run, run and run, and nothing else ... Almost AC automaton naked question ah (hansbug: Incredibly faster than phile Yes, I 2352ms, you are 3000+ms Oh, phile: Ah hehe well I am also drunk)
1 type2Point=^node;3Node=Record4 st:ansistring;5 Ex:longint;6 Jump,fat:point;7NextArray['A'..'Z'] ofPoint ;8     End;9 varTen I,j,k,l,m,n:longint; One Head,p1,p2:point; A s1:ansistring; -A:Array[0..2000000] ofLongint; - functionGetpoint:point; the          varP1:point;c1:char; -          begin - new (p1); -p1^.ex:=0; +p1^.st:="'; -p1^.fat:=Nil; +p1^.jump:=head; A                forc1:='A'  to 'Z'  Dop1^.next[c1]:=Nil; atgetpoint:=P1; -          End; - procedureins (s1:ansistring); -           var - s2:ansistring; - I,j,k,l:longint; in P1,p2:point; -           begin top1:=head;s2:="'; +                 fori:=1  toLength (S1) Do -                    begin thes2:=s2+S1[i]; *                         ifp1^.next[s1[i]]=Nil  Then $                            beginPanax Notoginsengp2:=GetPoint; -p2^.st:=S2; thep2^.fat:=P1; +p1^.next[s1[i]]:=p2;; A                            End; thep1:=P1^.next[s1[i]]; +                         ifI=length (S1) Thenp1^.ex:=1; -                    End; $           End; $ procedureLinkit; -           var - I,j,k,l,f,r:longint; the p1,p2:point; c1:char; -D:Array[0..10000] ofPoint ;Wuyi           begin thef:=1; r:=2; -d[1]:=head; Wu                 whileF<r Do -                      begin About                            forc1:='A'  to 'Z'  Do $                               begin -                                    ifD[f]^.next[c1]<>Nil  Then -                                       begin -d[r]:=D[F]^.NEXT[C1]; A                                            if(D[f]<>head) Then +                                               begin thep2:=D[f]^.jump; -                                                     while(p2^.next[c1]=Nil) and(P2<>head) Dop2:=P2^.jump; $                                                    ifP2^.next[c1]<>Nil  Thend[r]^.jump:=P2^.NEXT[C1]; the                                               End; the Inc (R); the                                       End; the                               End; - Inc (f); in                      End; the           End; the functioncal (s1:ansistring): Longint; About          var the I,j,k,l:longint; the P1,p2:point; the          begin +p1:=head;l:=0; -Fillchar (A,sizeof (a),0); thea[0]:=1;Bayi                fori:=1  toLength (S1) Do the                   begin the                        ifp1^.next[s1[i]]=Nil  Then -                            begin -                                  while(p1^.next[s1[i]]=Nil) and(P1<>head) Dop1:=P1^.jump; the                                 ifP1^.next[s1[i]]<>Nil  Thenp1:=P1^.next[s1[i]] the                            End the                        Elsep1:=P1^.next[s1[i]]; thep2:=P1; -                         whileP2<>head Do the                              begin the                                   if(p2^.ex=1) and(A[i-length (P2^.st)]=1) Then the                                      begin94a[i]:=1; thel:=I;break; the                                      End; thep2:=P2^.jump;98                              End; About                   End; - exit (l);101          End;102 103 begin104READLN (n,m); head:=GetPoint; thehead^.jump:=head;106       fori:=1  toN Do107          begin108 readln (S1);109 ins (upcase (S1)); the          End;111 Linkit; the       fori:=1  toM Do113          begin the readln (S1); the Writeln (Cal (UpCase (S1))); the          End;117 Readln;118 End.119            

1212: [Hnoi2004]l language

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.