Algorithm template--ac automata

Source: Internet
Author: User

Implement function--input n,m, provide a dictionary of n words, and then in the last input of M string to match (about AC automata algorithm, here no longer repeat, see: Aho-corasick multi-mode matching algorithm, AC automata detailed . Given the fact that dictionaries are sometimes fairly sparse, the chi and bro pointers are introduced for optimization-the principle is similar to the adjacency table, which is inherently consistent with the next array, except that Chi and bro are used to traverse a child node under a node. Next is used to query for the desired child nodes under a node)

1 type2Point=^node;3Node=Record4 ex:longint;st:ansistring;5 Ct:char;6 Fat,jump,chi,bro:point;7NextArray['A'..'Z'] ofPoint ;8     End;9 varTen I,j,k,l,m,n:longint; One Head,p:point; A s1,s2:ansistring; - functionGetpoint:point;inline; -          varP:point;c1:char; the          begin - new (p); -p^.ex:=0;p ^.st:="'; -P^.CT:=CHR (0); +p^.bro:=Nil;p ^.chi:=Nil; -p^.fat:=Nil;p ^.jump:=head; +                forc1:='A'  to 'Z'  Dop^.next[c1]:=Nil; A exit (p); at          End; - procedureins (s1:ansistring;x:longint); inline; -           varP:point;s2:ansistring;i:longint; -           begin -P:=head; s2:="'; -                 fori:=1  toLength (S1) Do in                    begin -s2:=s2+S1[i]; to                         ifp^.next[s1[i]]=Nil  Then +                            begin -p^.next[s1[i]]:=GetPoint; thep^.next[s1[i]]^.fat:=p; *p^.next[s1[i]]^.st:=S2; $p^.next[s1[i]]^.ct:=S1[i];Panax Notoginsengp^.next[s1[i]]^.bro:=P^.chi; -p^.chi:=P^.next[s1[i]]; the                            End; +p:=P^.next[s1[i]]; A                    End; the                ifp^.ex=0  Thenp^.ex:=x; +           End; - procedureLinkit;inline; $           varI,j,k,l,f,r:longint; $D:Array[0..10000] ofPoint ; - P,p1,p2:point; -           begin thef:=1; r:=2;d [1]:=head; -                 whileF<r DoWuyi                      begin thep:=D[f]^.chi; -                            whileP<>Nil  Do Wu                                 begin -d[r]:=p; About                                      ifD[f]<>head Then $                                         begin -p1:=D[f]^.jump; -                                               whileP1<>head Do -                                                    begin A                                                         ifP1^.next[p^.ct]<>Nil  ThenBreak ; +p1:=P1^.jump; the                                                    End; -                                              ifP1^.next[p^.ct]<>Nil  Thenp^.jump:=P1^.next[p^.ct]; $                                         End; the Inc (R); thep:=P^.bro; the                                 End; the Inc (f); -                      End; in           End; the procedureFit (s1:ansistring), inline; the           varP,p1:point;i:longint; About           begin thep:=head; the                 fori:=1  toLength (S1) Do the                    begin +                         ifp^.next[s1[i]]=Nil  Then -                            begin the                                  while(p^.next[s1[i]]=Nil) and(P<>head) Dop:=P^.jump;Bayi                                 ifP^.next[s1[i]]<>Nil  Thenp:=P^.next[s1[i]]; the                            End the                         Elsep:=P^.next[s1[i]]; -p1:=p; -                          whileP1<>head Do the                               begin the                                    ifP1^.ex<>0  ThenWriteln ('No.', P1^.ex,' ', P1^.st,'From :', I-length (p1^.st) +1); thep1:=P1^.jump; the                               End; -                    End; the           End; the begin the readln (n,m);94head:=getpoint;head^.jump:=head; the       fori:=1  toN Do the          begin the readln (S1);98 ins (upcase (S1), i); About          End; - Linkit;101       fori:=1  toM Do102          begin103 readln (S1);104 Fit (upcase (S1)); the          End;106 End.107           

Algorithm template--ac automata

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.