String match
Time limit:1 Sec Memory limit:128 MB
submit:214 solved:81
Description
Give you two strings, A, B, please output a string in a string that appears several times.
Input
Multiple sets of test data, each set of two strings entered. The length of the string is <= 1000000.
Output
The number of times that output B appears in a.
Sample Input
AAA AA
Sample Output
1
The number of occurrences of a substring in a parent string, with no overlapping of strings
#include <stdio.h>#include <iostream>#include <math.h>#include <stdlib.h>#include <ctype.h>#include <algorithm>#include <vector>#include <string.h>#include <queue>#include <stack>#include <set>#include <map>#include <sstream>#include <time.h>#include <malloc.h>using namespace STD;voidGet_next (CharX[],intMintNext[]) {intI, J; j = next[0] = -1; i =0; while(I < m) { while(-1! = J && X[i]! = x[j]) j = next[j]; Next[++i] = ++j; }}intnext[1001000];intKMP (CharX[],intMCharY[],intN//x Mode string y main string{intI, j, ans =0; i = j =0; Get_next (x, M, next); while(I < N) { while(-1! = J && Y[i]! = x[j]) j = next[j]; i++; j + +;if(J >= m) {ans++; j =0; } }returnAns;}Chara[1000100], b[1000100];intMain () { while(scanf('%s ', a)!=eof) {scanf('%s ', b);intn =strlen(a);intm =strlen(b);//KMP (b, M, A, n); printf("%d\n", KMP (b, M, A, n)); }}
String matches "KMP"