mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-02-10 14:05:17 +08:00
Merge pull request #277 from mahbubcseju/master
Added knuth pratt morris algorithm for finding string match.
This commit is contained in:
64
String/knuth_morris_pratt.cpp
Normal file
64
String/knuth_morris_pratt.cpp
Normal file
@@ -0,0 +1,64 @@
|
||||
/*
|
||||
The Knuth-Morris-Pratt Algorithm for finding a pattern within a piece of text
|
||||
with complexity O(n + m)
|
||||
1) Preprocess pattern to identify any suffixes that are identical to prefixes
|
||||
This tells us where to continue from if we get a mismatch between a character in our pattern
|
||||
and the text.
|
||||
2) Step through the text one character at a time and compare it to a character in the pattern
|
||||
updating our location within the pattern if necessary
|
||||
*/
|
||||
|
||||
#include<iostream>
|
||||
#include<vector>
|
||||
#include<string>
|
||||
using namespace std;
|
||||
vector<int> getFailureArray(string pattern){
|
||||
int pattern_length=pattern.size();
|
||||
vector<int>failure(pattern_length+1);
|
||||
failure[0]=-1;
|
||||
int j=-1;
|
||||
for(int i=0; i<pattern_length; i++){
|
||||
while(j!=-1&&pattern[j]!=pattern[i]){
|
||||
j=failure[j];
|
||||
}
|
||||
j++;
|
||||
failure[i+1]=j;
|
||||
}
|
||||
return failure;
|
||||
}
|
||||
bool kmp(string pattern,string text){
|
||||
int text_length=text.size(),pattern_length=pattern.size();
|
||||
vector<int>failure=getFailureArray(pattern);
|
||||
int k=0;
|
||||
for(int j=0; j<text_length; j++){
|
||||
while(k!=-1&&pattern[k]!=text[j]){
|
||||
k=failure[k];
|
||||
}
|
||||
k++;
|
||||
if(k==pattern_length)return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
|
||||
string text="alskfjaldsabc1abc1abc12k23adsfabcabc";
|
||||
string pattern="abc1abc12l";
|
||||
if(kmp(pattern,text)==true){
|
||||
cout<<"Found"<<endl;
|
||||
}
|
||||
else{
|
||||
cout<<"Not Found"<<endl;
|
||||
}
|
||||
text="abcabc";
|
||||
pattern="bca";
|
||||
if(kmp(pattern,text)==true){
|
||||
cout<<"Found"<<endl;
|
||||
}
|
||||
else{
|
||||
cout<<"Not Found"<<endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user