-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathcode_3.cpp
66 lines (62 loc) · 1.63 KB
/
code_3.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
//
// code_3.cpp
// Algorithm
//
// Created by Mohd Shoaib Rayeen on 23/11/18.
// Copyright © 2018 Shoaib Rayeen. All rights reserved.
//
#include <iostream>
#include <string>
using namespace std;
#define d 256
bool search(string text , string pattern) {
long M = pattern.length();
long N = text.length();
bool flag = false;
int q = 29; // Any PRIME Number according to pattern value
int j;
int pattern_hash = 0;
int text_hash = 0;
int h = 1;
for (int i = 0; i < M-1; i++) {
h = (h*d)%q;
}
//calculating hash value for pattern and text
for (int i = 0; i < M; i++) {
pattern_hash = (d*pattern_hash + pattern[i])%q;
text_hash = (d*text_hash + text[i])%q;
}
for (int i = 0; i <= N - M; i++) {
if ( pattern_hash == text_hash ) {
for (j = 0; j < M; j++) {
if (text[i+j] != pattern[j])
break;
}
if (j == M) {
cout << "Pattern found at index\t:\t" << i << "\n";
flag = true;
}
}
// calculating hash value for text
if ( i < N-M ) {
text_hash = (d*(text_hash - text[i]*h) + text[i+M])%q;
if (text_hash < 0) {
text_hash = (text_hash + q);
}
}
}
return flag;
}
int main() {
string text , pattern;
cout << "\nEnter Text\t\t\t\t:\t";
getline(cin , text);
cout << "\nEnter Pattern\t\t\t:\t";
getline(cin , pattern);
cout << endl;
if ( !search(text ,pattern) ) {
cout << "\nPattern Not Found\n";
}
cout << endl;
return 0;
}