Hihocoder 1015 KMP (for multiple locations)

Source: Internet
Author: User

#1015: KMP Algorithm time limit:1000msSingle Point time limit:1000msMemory Limit:256MBDescribe

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 <iostream> #include <string> #include <cstdio> #include <cstring> #include <    Algorithm> #include <stack>using namespace std;void get_next (string t, int next[]) {int J, K; j=0;    K=-1;    Next[0]=-1;    int len=t.size ();            while (J<len) {if (K==-1 | | t[j]==t[k]) {j + +;            k++;        Next[j]=k;    } else k=next[k];    }}int KMP (string s, string t, int next[]) {int I, J; i=0;    j=0;    int len1=s.size ();    int len2=t.size ();    int cnt=0;            while (I&LT;LEN1) {if (J==-1 | | s[i]==t[j]) {i++;        j + +;        } else if (J&LT;LEN2) j=next[j];            if (j==len2) {cnt++;        J=NEXT[LEN2]; }} return cnt;}    int main () {string S, T;    int I, J;    int len1, len2;    int next[10002];    int tt;    cin>>tt;        while (tt--) {cin>>t;        cin>>s; Len1=s.sIze ();        Len2=t.size ();        Get_next (t, next);    printf ("%d\n", KMP (S, T, next)); } return 0;}

Hihocoder 1015 KMP (for multiple locations)

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.