-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLPS_ForPattern.cpp
53 lines (53 loc) · 925 Bytes
/
LPS_ForPattern.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
// Created by Anushk Gautam
/*
GitHub Repo : https://github.com/Anushk2001/Optimisation-of-Pattern-Matching
Optimisation of Pattern Matching
*/
#include <bits/stdc++.h>
using namespace std;
vector<int> LPS;
void compute_LPS(string &S)
{
LPS.resize(S.length() + 1);
int len = 0;
LPS[0] = 0;
int L = S.length();
int i = 1;
while (i < L)
{
if (S[i] == S[len])
{
len++;
LPS[i] = len;
i++;
}
else
{
if (len == 0)
{
LPS[i] = 0;
i++;
}
else
{
len = LPS[len - 1];
}
}
}
}
void output_LPS(string &S)
{
int L = S.length();
for (int i = 0; i < L; i++)
{
cout << LPS[i] << " ";
}
}
int main()
{
string S;
cin >> S;
compute_LPS(S);
output_LPS(S);
return 0;
}