-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathNaiveAlgorithm.cpp
More file actions
42 lines (35 loc) · 882 Bytes
/
NaiveAlgorithm.cpp
File metadata and controls
42 lines (35 loc) · 882 Bytes
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
#include <iostream>
#include <string>
using namespace std;
void search(string& pat, string& txt) {
int M = pat.size();
int N = txt.size();
// A loop to slide pat[] one by one
for (int i = 0; i <= N - M; i++) {
int j;
// For current index i, check for pattern match
for (j = 0; j < M; j++) {
if (txt[i + j] != pat[j]) {
break;
}
}
// If pattern matches at index i
if (j == M) {
cout << "Pattern found at index " << i << endl;
}
}
}
// Driver's Code
int main() {
// Example 1
string txt1 = "AABAACAADAABAABA";
string pat1 = "AABA";
cout << "Example 1: " << endl;
search(pat1, txt1);
// Example 2
string txt2 = "agd";
string pat2 = "g";
cout << "\nExample 2: " << endl;
search(pat2, txt2);
return 0;
}