-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathSolution.cpp
More file actions
46 lines (40 loc) · 1.46 KB
/
Solution.cpp
File metadata and controls
46 lines (40 loc) · 1.46 KB
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
#include <iostream>
#include <string>
using namespace std;
int checkStr(string pattern, string text, int start, int len) // checks if the strings match
{
for (int i=0; i<len; i++) // O(n)
if (text[start+i] != pattern[i]) return 0; // mismatch found
cout<<"Pattern found at index: "<<start<<endl;
return 1; // if pattern found returns 1
}
int main()
{
string text, pattern;
getline(cin, text);
getline(cin,pattern);
int flag=0, textLen = text.length(), patternLen = pattern.length(), patternVal=0, windowVal=0;
for (int i=0; i<patternLen; i++)
{
patternVal += int(pattern[i]) * 7; // this would be the hash function
windowVal += int(text[i]) * 7;
}
if (windowVal == patternVal) // initial check
{
if(checkStr(pattern, text, 0, patternLen)) // if pattern found
flag = 1; // so last line not printed
}
for (int i=patternLen; i<textLen; i++) // O(n)
{
windowVal -= (int(text[i-patternLen]) * 7); // rolling hash
windowVal += (int(text[i]) * 7);
if (windowVal == patternVal)
{
if(checkStr(pattern, text, i-patternLen+1, patternLen)) // O(mn) in worst case; O(m+n) if I'm a good programmer
flag = 1;
}
}
// I'm sure the initial checkStr can be combined with the loop by changing where the loop starts and ends
// but it's midnight and I'm tired
if (!flag) cout<<"Pattern not found!"<<endl;
}