/* <Br/> after one year, learn KMP again to prepare for the AC automatic machine. <Br/> Yan Weimin's Data Structure p82 is clear. <Br/> if you do not want to learn how to pull the template directly, it is suitable for Matching Multiple Target strings with one string. <Br/> */<br/> # include <stdio. h> <br/> # include <string. h> <br/> const int maxn = 1000; <br/> char a [maxn], B [maxn]; <br/> int next [maxn]; <br/> void get_next (char * s) <br/>{< br/> next [1] = 0; <br/> int I = 1, j = 0, L = strlen (s --); <br/> while (I <L) {<br/> If (j = 0 | s [I] = s [J]) {<br/> I ++; <br/> J ++; <br/> next [I] = s [I]! = S [J]? J: Next [J]; <br/>}else {<br/> J = next [J]; <br/>}< br/> int KMP (char * a, char * B) <br/> {<br/> int n = strlen (A --), M = strlen (B --); <br/> int I = 0, j = 0; <br/> while (I <= N & J <= m) {<br/> If (j = 0 | A [I] = B [J]) {<br/> I ++; <br/> J ++; <br/>} else {<br/> J = next [J]; <br/>}< br/> return j> M? I-m-1:-1; <br/>}</P> <p> void _ test () <br/>{< br/> scanf ("% S % s", a, B); <br/> get_next (B ); <br/> printf ("% d/N", KMP (A, B); <br/>}< br/> int main () <br/>{< br/>__ test (); <br/>}