-
Notifications
You must be signed in to change notification settings - Fork 7
/
Zalgo.cpp
68 lines (56 loc) · 892 Bytes
/
Zalgo.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
57
58
59
60
61
62
63
64
65
66
67
68
//Program for pattern matching using z algorithm
#include <iostream>
#include <string>
using namespace std;
//function to search the pattern
void search(int *z, string s, int n, int p)
{
for (int i = 0; i < n; ++i)
{
if(z[i]==p)
cout<<"Pattern found at index "<<i-p-1<<endl;
}
}
//function to construct Z array
void constructzarray(int *z, string s, int n)
{
int L,R,k;
L=R=0;
for (int i = 1; i < n; ++i)
{
if(i>R)
{
L=R=i;
while(R<n && s[R-L]==s[R])
R++;
z[i]=R-L;
R--;
}
else
{
k=i-L;
if(z[k]<R-i+1)
z[i]=z[k];
else
{
L=i;
while(R<n && s[R-L] ==s[R])
R++;
z[i]=R-L;
R--;
}
}
}
}
//driver function
int main()
{
string str="abacdbabaa";
string pat="aba";
string concat=pat+"$"+str;
int m=pat.length();
int n=concat.length();
int z[n]={0};
constructzarray(z,concat,n);
search(z,concat,n,m);
}