-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNaiveApproach.cpp
56 lines (55 loc) · 1.01 KB
/
NaiveApproach.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
// 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;
bool isMatching(int &N)
{
if (N == 0)
return false;
else
return true;
}
void print_Indexes(vector<int> &res)
{
// 0-Base Indexing
int N = res.size();
if (!isMatching(N))
{
cout << "Pattern Not Found!\n";
}
else
{
cout << "Pattern Found At Indexes:\n";
for (int i = 0; i < N; i++)
{
cout << res[i] << " ";
}
cout << "\n";
}
}
void string_matching(string &S, string &s)
{
vector<int> res;
int n = S.length();
int m = s.length();
for (int i = 0; i <= n - m; i++)
{
string temp = S.substr(i, m);
if (temp == s)
{
res.push_back(i);
}
}
print_Indexes(res);
}
int main()
{
string S, s;
cin >> S;
cin >> s;
string_matching(S, s);
return 0;
}