forked from Priyansh19077/CP-Templates
-
Notifications
You must be signed in to change notification settings - Fork 0
/
3.KMP_Algo_Pattern_Matching_Strings.cpp
93 lines (93 loc) · 1.89 KB
/
3.KMP_Algo_Pattern_Matching_Strings.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
//KMP Algorithm to check pattern is present as a substring in the original string or not
//Frist find out the longest prefix which is also a suffix and ends at the ith index
//store this in the form of an array
//O(N+M)
#include<bits/stdc++.h>
#include<algorithm>
#include<unordered_map>
#include<vector>
#include<unordered_set>
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<map>
using namespace std;
#define fastio() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define MOD 1000000007
#define MOD1 998244353
#define nline "\n"
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
typedef long long ll;
typedef unsigned long long ull;
typedef priority_queue<int, vector<int>, function<bool(int, int)>> pq_func;
void longest_prefix_suffix(int* lps, string pattern, int m)
{
lps[0] = 0;
int i = 1;
int j = 0;
while (i < m)
{
if (pattern[i] == pattern[j])
{
lps[i] = j + 1;
j++;
i++;
}
else
{
if (j != 0)
j = lps[j - 1];
else
{
lps[i] = 0;
i++;
}
}
}
return;
}
bool kmp(string a, string pattern, int n, int m)
{
int *lps = new int[m];
longest_prefix_suffix(lps, pattern, m);
//longest_prefix_suffix array has been calculated now
int i = 0;
int j = 0;
while (i < n && j < m)
{
if (pattern[j] == a[i])
{
i++;
j++;
}
else
{
if (j != 0)
j = lps[j - 1];
else
i++;
}
}
return j == m;
}
int main()
{
fastio();
#ifndef ONLINE_JUDGE
freopen("Input.txt", "r", stdin);
freopen("Output.txt", "w", stdout);
freopen("Error.txt", "w", stderr);
#endif
string s, pattern;
cin >> s >> pattern;
//make the prefix-suffix array
int n = s.length();
int m = pattern.length();
bool search = kmp(s, pattern, n, m);
//search variable stores the answer to the search according to kmp algorithm
return 0;
}