HIHOCODER-1015-KMP algorithm

Source: Internet
Author: User

#1015: KMP Algorithm time limit:1000msSingle Point time limit:1000msMemory Limit:256MB
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


KMP name, has not been able to touch, today only tasted his beauty!


Look at the book is not very clear, and then go to see someone else to write the blog, and feel pretty good (blog dot here)


The problem is naked kmp, practicing practiced hand.


AC Code:

#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include < Cmath>using namespace Std;char Mo[10005];char str[1000005];int n;int Main () {scanf ("%d", &n), while (n--) {scanf (" %s%s ", MO, str); int next[10005] = { -1};int i = 0, j =-1, len = strlen (MO), while (I < len) {//Get Next function if (j = =-1 | | mo [i] = = Mo[j]) Next[++i] = ++j;else j = next[j];}  int ans = 0, len1 = strlen (str), i = j = 0;while (I < LEN1) {//The number of times the pattern string appears in the original string if (j = =-1 | | str[i] = = Mo[j]) ++i, ++j;else j = Next[j];if (j = = Len) ans++;} printf ("%d\n", ans);} return 0;}












HIHOCODER-1015-KMP algorithm

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.