-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubstring matching.cpp
More file actions
74 lines (68 loc) · 1.42 KB
/
Copy pathsubstring matching.cpp
File metadata and controls
74 lines (68 loc) · 1.42 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <bits/stdc++.h>
using namespace std;
//computing LPS array
void computeLPS(string text,string pattern,int lps[])
{
int i=1;
int len;
lps[0]=0;
int m=pattern.size();
while(i!=m)
{
len=lps[i-1];
while(pattern[i]!=pattern[len] && len>0)
len=lps[len-1];
if(pattern[i]=pattern[len])
lps[i]=len+1;
else
lps[i]=0;
i++;
}
}
//implementing KMP algorithm
vector <int> KMPsearch(string text,string pattern)
{
int n=text.size();
int m=pattern.size();
//array lps[i] stores the length of the longest proper prefix suffix of pattern[0...i].
int lps[m];
computeLPS(text,pattern,lps);
int i=0;
int j=0;
vector <int> positions;
while(i!=n)
{
if(text[i]==pattern[j])
{
i++;
j++;
if(j==m)
positions.push_back(i-j);
}
else if(j>0)
j=lps[j-1];
else
i++;
}
return positions;
}
int main()
{
string text="aaabbaaaa";
string pattern="bb";
/*
for(i=0;i<10;i++)
pattern+=pattern;
for(i=0;i<120;i++)
text+=text;
cout << text.size();
*/
vector <int> positions=KMPsearch(text,pattern);
if(positions.size()==0)
cout << "Not Found";
else
{
for(int i=0;i<positions.size();i++)
cout << positions[i] << " ";
}
}