String matches "KMP"

Source: Internet
Author: User

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"

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.