Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
abdurrezzak authored Nov 9, 2017
1 parent fec9463 commit 90facee
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions 19.KMP.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* This program takes a text and a pattern and searches the pattern inside the text
* using Knuth- Morris- Pratt Algorithm
*
* Coded by: Abdurrezak Efe
*
* */
#include <iostream>

using namespace std;

int fail[10000]; //assuming pattern has a length less than or equal to 10000

void failure(string s)
{
int i=1;
int j=0;
while(i<s.size())
{
if(s[i] == s[j])
fail[i] = j+1, i++, j++;
else if(j>0)
j = fail[j-1];
else
fail[i] = 0, i++;
}
}

int kmp(string text, string pattern)
{
int i=0;
int j=0;
while(i<text.size())
{
if(text[i] == pattern[j])
if(j == pattern.size()-1)
return i-j; // we have a match
else
i++,j++;
else
if(j>0)
j = fail[j-1];
else
i++;
}
return -1;

}

int main()
{
string text, pattern;
cin >> text >> pattern;

cout << kmp(text, pattern) << endl;
}

0 comments on commit 90facee

Please sign in to comment.