-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNaive_Pattern_Matching_Algorithm.cpp
More file actions
69 lines (44 loc) · 1.21 KB
/
Naive_Pattern_Matching_Algorithm.cpp
File metadata and controls
69 lines (44 loc) · 1.21 KB
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
#include <iostream>
#include <string>
#include <vector>
#include <time.h>
using namespace std;
void NaivePatternMatchingAlgorithm(string text, string pattern){
vector<int> match_pos;
clock_t start = clock();
for(int i = 0; i < text.length() - pattern.length() + 1; i++){
if(text.substr(i,pattern.length()) == pattern){
match_pos.emplace_back(i);
}
}
clock_t end = clock();
double elapsed = double(end - start) / CLOCKS_PER_SEC;
cout << "Elapsed time: " << elapsed << " seconds." << std::endl;
if(match_pos.size() > 0){
cout << "Matches found at Positions: ";
for(int &i: match_pos){
cout << i+1 << " ";
}
cout << endl;
}
else{
cout << "No match found." << endl;
}
}
int main(){
string text;
string pattern;
cout << "Enter Your Text: ";
getline(cin,text);
cout << "Enter Your Pattern: ";
getline(cin,pattern);
if(text.length() < pattern.length()){
cerr << "Error: Length of text should be greater than pattern." << endl;
}
else{
cout << endl;
NaivePatternMatchingAlgorithm(text,pattern);
cout << endl;
}
return 0;
}