-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsunday_pattern_alg.cpp
97 lines (76 loc) · 1.91 KB
/
sunday_pattern_alg.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
#include <stdio.h>
#include <string.h>
#include <fstream>
#include <vector >
#include <iostream>
#include <string>
#include <bits/stdc++.h>
#include <windows.h>
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;
}
static int Max(int a, int b) {
return a >= b ? a : b;
}
static void BadCharHeuristic(string str, int size, int* badChar) {
int i;
for (i = 0; i < 256; i++)
badChar[i] = -1;
for (i = 0; i < size; i++)
badChar[(int)str[i]] = i;
}
static vector<int> SearchString(string str, string pat) {
vector<int> retVal;
int m = pat.length();
int n = str.length();
int* badChar = new int[256];
BadCharHeuristic(pat, m, badChar);
int s = 0;
while (s <= (n - m)){
int j = m - 1;
while (j >= 0 && pat[j] == str[s + j])
--j;
if (j < 0){
retVal.push_back(s);
s += (s + m < n) ? m - badChar[str[s + m]] : 1;
}
else{
s += Max(1, j - badChar[str[s + j]]);
}
}
delete[] badChar;
return retVal;
}
int main(){
ifstream myfile("data.txt");
string line;
int currentLine=0;
StartCounter();
if(myfile.is_open()){
while(getline(myfile,line)){
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;
}