-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path19.KMP.cpp
56 lines (49 loc) · 1 KB
/
19.KMP.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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;
}