2296:KMP Pattern matching Two (string)Time limit: 1 Sec Memory limit: 128 MB
Submitted: 29 Settlement: 17
Topic Description
Enter a main string and a substring, match with KMP, ask for a few matches to succeed, if not successful, then output 0 input
Enter a main string and a substring output
Matching number of trip examples input
Ababcabcacbab
ABCAC
Sample Output
3
lost in the valley of the birds, flying alone in this huge world, but do not know where to fly to ...
#include <stdio.h> #include <string.h> #define SIZEMAX typedef struct {char Data[sizemax];
int length;
}sqstring;
void GetNext (sqstring s,int next[]) {int j=0,k=-1;
Next[0]=-1; while (j<s.length-1) {if (k==-1| |
S.DATA[J]==S.DATA[K]) j++,k++,next[j]=k;
else K=next[k];
} void Strassign (Sqstring &s,char cstr[]) {int i;
For (i=0 i< (int) strlen (CStr); i++) s.data[i]=cstr[i];
S.length=i;
int KMP (sqstring s,sqstring t,int next[]) {int i=0,j=0,ci=1; while (i<s.length&&j<t.length) {if (j==-1| |
S.DATA[I]==T.DATA[J]//pattern string and main string corresponding character match succeeded {i++;j++; else j=next[j],ci++;
The pattern string pointer returns to the corresponding next, matches the number +1} if (j>=t.length) return CI;
else return 0;
int main () {sqstring s,t;
int next[sizemax]={-1};
Char C[sizemax],d[sizemax];
Gets (c);
Gets (d); Strassign (S,C); Set up a string strassign(T,D); GetNext (T,next); Get Next value int ci=kmp (s,t,next);
Get a match to the location printf ("%d\n", CI);
return 0;
}