Directory topic description algorithm Core code
Directory 1. Topic Description
The title comes from hihocoder:http://hihocoder.com/problemset/problem/1015
1015:KMP algorithm
Time limit: 1000ms
Single Point time: 1000ms
Memory Limit: 256MB
Describe
Little Hi and small ho is a pair of good friends, born in the information society they have a great deal of interest in programming, they agreed to help each other, in the programming of the learning path along together.
This day, they met a 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...... Text (pattern string). ”
Little Hi and Little ho think about it, think can only think of very simple practice, but also feel that since the crab said so, it will certainly not so easy to let them answer, so they can only say: "Sorry, Mr. Crab, we can only think of time complexity (text length * Special text length) method, That is, for each pattern string to be judged separately, and then to enumerate the starting position in turn and check to see if it matches, but that's not the way you want it to be. ”
The crab nodded and said: "It seems your level has yet to be improved, so, if I say only a special text, you can do it." “
Small Ho is a bit dizzy at this time, but little hi quickly opens the way: I know. This is a classic pattern-matching problem. You can use the KMP algorithm for solving. “
Crab satisfied nodded, to Little Hi said: "Since you know it will be good 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.
Hint one: the thought 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 sets.
The next n*2 line, each two lines represents a test data. In each test data, the first behavior pattern string consists of no more than 10^4 capitals, and the second behavior string consists of no more than 10^6 capitals.
of which n<=20
Output
For each test data, output a line of ans in the order in which they appear in the input, representing the number of times the pattern string appears in the original string.
Sample input
5
HA
hahaha
wqn
wqn
ADA
adadada
bababb
Bababababababababb Addaadaaddaaadaad
Sample output
3
1
3
1
0
2. Algorithm Core
This topic is calculated as the number of times the pattern string appears in the original string. We are comparing 3 by adding a special character $ when the pattern string goes to the last time in the pattern string .
#include <iostream> #include <vector> #include <string> using namespace std;
void Nextarraycal (String &str,vector<int> &next) {int j=0,k=-1;
Next[0]=-1; while (J<str.size ()-1) {if (k==-1| |
Str[j]==str[k]) {j++,k++;
if (str[j]!=str[k]) next[j]=k;
else Next[j]=next[k];
Next[j]=k;
else K=next[k];
an int kmpindex (string &str,string&pat) {Vector<int>next (Pat.size ());
Nextarraycal (Pat,next);
int i=0,j=0;
int res=0;
int strsize=str.size (), patsize=pat.size (); while (i<strsize) {if (j==-1| |
Str[i]==pat[j]) {i++;j++;
} else{J=next[j];
} if (j>=patsize-1) {++res;
J=NEXT[PATSIZE-1];
} return res;
int main () {int tn;
cin>>tn; StrinG str;
String Pat;
Str.reserve (1000008);
Pat.reserve (10008);
for (int ye=0;ye<tn;++ye) {cin>>pat;
pat+= "$";
cin>>str;
cout<< Kmpindex (Str,pat) <<endl; }
}