-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrabin_karp.cpp
156 lines (126 loc) · 3.36 KB
/
rabin_karp.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include<stdio.h>
#include<string.h>
#include <fstream>
#include <iostream>
#include <string>
#include <fstream>
#include <bits/stdc++.h>
#include<cstring>
#include<cstdio>
#include <fstream>
#include <ctime>
#include <cstdlib>
#include <windows.h>
#include <iomanip>
// d is the number of characters in input alphabet
#define d 256
using namespace std;
double PCFreq = 0.0;
__int64 CounterStart = 0;
void StartCounter()
{
LARGE_INTEGER li;
if(!QueryPerformanceFrequency(&li))
cout << "QueryPerformanceFrequency failed!\n";
PCFreq = double(li.QuadPart)/1000000.0;
QueryPerformanceCounter(&li);
CounterStart = li.QuadPart;
}
double GetCounter()
{
LARGE_INTEGER li;
QueryPerformanceCounter(&li);
return double(li.QuadPart-CounterStart)/PCFreq;
}
/* pat -> pattern
txt -> text
q -> A prime number
*/
void search(char pat[], char txt[], int q)
{
int M = strlen(pat);
int N = strlen(txt);
int i, j;
int p = 0; // hash value for pattern
int t = 0; // hash value for txt
int h = 1;
// The value of h would be "pow(d, M-1)%q"
for (i = 0; i < M-1; i++){
h = (h*d)%q;
}
for (i = 0; i < M; i++){
p = (d*p + pat[i])%q;
t = (d*t + txt[i])%q;
}
// Slide the pattern over text one by one
for (i = 0; i <= N - M; i++){
if ( p == t ){
/* Check for characters one by one */
for (j = 0; j < M; j++){
if (txt[i+j] != pat[j])
break;
}
// if p == t and pat[0...M-1] = txt[i, i+1, ...i+M-1]
if (j == M){
//printf("Pattern found at index %d \n", i);
}
}
if ( i < N-M ){
t = (d*(t - txt[i]*h) + txt[i+M])%q;
if (t < 0)
t = (t + q);
}
}
}
static vector<int> SearchString(string A, string B) {
vector<int> retVal;
unsigned long siga = 0;
unsigned long sigb = 0;
unsigned long Q = 100007;
unsigned long D = 256;
int ALength = A.length();
int BLength = B.length();
for (int i = 0; i < BLength; ++i)
{
siga = (siga * D + (unsigned long)A[i]) % Q;
sigb = (sigb * D + (unsigned long)B[i]) % Q;
}
if (siga == sigb)
retVal.push_back(0);
unsigned long pow = 1;
for (int k = 1; k <= BLength - 1; ++k)
pow = (pow * D) % Q;
for (int j = 1; j <= ALength - BLength; ++j)
{
siga = (siga + Q - pow * (unsigned long)A[j - 1] % Q) % Q;
siga = (siga * D + (unsigned long)A[j + BLength - 1]) % Q;
if (siga == sigb)
if (A.substr(j, BLength) == B)
retVal.push_back(j);
}
return retVal;
}
int main()
{
char pat[] = "netwothree";
ifstream myfile("data.txt");
string line;
int currentLine = 0;
StartCounter();
if(myfile.is_open()){
while(getline(myfile,line)){
char *a=new char[line.size()+1];
a[line.size()]=0;
memcpy(a,line.c_str(),line.size());
search(pat,a,11);
currentLine++;
/*vector<int> value =*/SearchString(line, "netwothreeftyujmnkoplkjhqertyujhnbytghjoplkjhbzxcvqerfgtyxcprtghjuiolpnetwothreeftyujmnkoplkjhqertyujhnbytghjoplkjhbzxcvqerfgtyxcprtghjuiolp");
// for(int x=0;x<value.size(); x++){
// cout<<"Line -> "<<currentLine <<". Pattern found at index :"<<value[x]<<endl;
// }
}
}
double var=GetCounter();
cout<<var<<endl;
return 0;
}