forked from markandey007/hacktoberfest_2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Z_Algo.cpp
71 lines (68 loc) · 1.51 KB
/
Z_Algo.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
69
70
71
/*
Time Complexity -> O(Pattern length + Text Length)
*/
#include <bits/stdc++.h>
using namespace std;
vector<int> z_array(string &str)
{
int len = str.size();
int l = 0, r = 0;
vector<int> z(len, 0);
// 1<=l<=i<=r --> ALWAYS satisfy
for (int i = 1; i < len; i++)
{
if (i > r)
{
// we have to calculate for this range
l = r = i;
while (r < len and str[r] == str[r - i])
{
r++;
}
z[i] = r - l;
r--;
}
else if (i <= r)
{
// means it possible to have inside the range
int rel_idx = i - l;
// chceking for inseide the range
if (i + z[rel_idx] <= r)
{
// then we can use previous
z[i] = z[rel_idx];
}
else
{
// then we have to change the range
l = i;
while (r < len and str[r] == str[r - l])
{
r++;
}
z[i] = r - l;
r--;
}
}
}
return z;
}
auto z_algo(string &pat, string &txt)
{
string str = pat + '$' + txt;
vector<int> z = z_array(str);
for (int i = 0; i < z.size(); i++)
{
if (z[i] == pat.size())
{
cout << i - pat.size() - 1 << " ";
}
}
}
int main()
{
string pat, txt;
cin >> pat >> txt;
z_algo(pat, txt);
return 0;
}