Describe
Small hi and small ho is a pair of good friends, born in the information society, they have a great interest in programming, they agreed to help each other, in the programming of learning along the road together.
This day, they met a river crab, so the crab to small hi and small ho put forward the classic question: "Small hi and small ho, you can not judge a paragraph of text (the original string) inside is not so some ... Special...... The text (pattern string)? "
Small hi and small ho thought carefully, think can only think of very simple practice, but also think that since the crab said, it is certainly not so easy to let them answer, so they can only say: "Sorry, Mr. Crab, we can only think of time complexity for (text length * Special text total length) method, That is, for each pattern string separate judgment, and then enumerate the starting position and check whether it can match, but this is not the way you want it? ”
Crab nodded, said: "It seems your level has yet to be improved, so, if I say only a special text , you can do it?" “
Little Ho was a little dizzy at this time, but little hi quickly opened his mouth and said, "I know!" This is a classic pattern matching problem! Can be solved using the KMP algorithm ! “
The crab satisfied nodded, to small hi said: "Since you know to do, you go to the Small Ho Church, next week I have important tasks to you!" “
"Guaranteed to complete the task!" "Little hi nodded."
Tip One: The idea of KMP
Hint two: use of next array
Tip three: How to solve next array
Input
The first line, an integer n, represents the number of test data groups.
The next n*2 line, each of the two lines represents a test data. In each test data, the first behavior pattern string consists of no more than 10^4 uppercase letters, the second behavior of the original string, consisting of no more than 10^6 uppercase letters.
where n<=20
Output
For each test data, output a line of ans in the order in which they appear in the input, indicating the number of times the pattern string appears in the original string.
-
-
Sample input
-
-
5HAHAHAHAWQNWQNADAADADADABABABBBABABABABABABABABBDADADDAADAADDAAADAAD
-
-
Sample output
-
-
31310
-
-
-
#include <stdio.h> #include <string.h> #include <stdlib.h>int KMP (char *ori,char *pat) {int num_pat= Strlen (PAT); int Num_ori=strlen (ORI); int next[num_pat+1];//starting from next[1] effective int i=0,j=0; int sum=0; if (Num_ori<num_pat) return 0;//find the next array memset ((int *) next,0, (num_pat+1) *sizeof (int)); for (I=1; i<num_pat; i++) {j=i; while (J > 0) {j = next[j]; if (pat[j] = = Pat[i]) {next[i+1] = j + 1; Break }}}//Match string for (i=0, j=0; i<num_ori; i++) {if ((J < Num_pat) && (ori[i] = = Pat[j]) J++;else{whil E (J > 0) {j = next[j];if (ori[i] = = Pat[j]) {j++;break;}}} if (j = = Num_pat) sum + +;} return sum;} int main (void) {char strori[1000001]; Char strpat[10001]; int n;//Test Group number scanf ("%d\n", &n); while (n--) {gets (STRPAT); Gets (Strori); printf ("%d\n", KMP (Strori,strpat)); } return 0;}
You can refer to my other articles for specific implementation principles of KMP.
-
-
The KMP here are continuous.
-
-
That
-
-
Mode string: DD
-
-
Original string: DDD
-
-
The output result is 2;