forked from snow28/pattern_search_algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
brute_force_alg.cpp
75 lines (54 loc) · 1.59 KB
/
brute_force_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
#include<cstring>
#include<iostream>
#include<cstdio>
#include <fstream>
#include <ctime>
#include <cstdlib>
#include <windows.h>
#include <iomanip>
using namespace std;
//running time counter algorithm
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;
}
void search (char *text , char *pat){
int n=strlen(text); // length of text
int m=strlen(pat); // length of pattern
int s,j;
for(s=0;s<=n-m;s++){ // Iteration over n-m+1 values of shift
for(j=0;j<m;j++){
if(pat[j]!=text[s+j]) break;
}
//if(j==m) cout<<"Pattern found at : "<<s<<endl; // COMMENT BEFORE COUNTING EXECUTING TIME not to spend time for displaying
}
}
int main(){
ifstream myfile("data.txt");
string line;
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(a,"netwothreeftyujmnkoplkjhqertyujhnbytghjoplkjhbzxcvqerfgtyxcprtghjuiolpnetwothreeftyujmnkoplkjhqertyujhnbytghjoplkjhbzxcvqerfgtyxcprtghjuiolp");
}
}
double var=GetCounter();
cout<<var<<endl;
return 0;
}