-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
720e59f
commit 6cf1c8f
Showing
5 changed files
with
223 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
**/.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
// implementation of invertion counting using merge sort | ||
|
||
#include<bits/stdc++.h> | ||
using namespace std; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
// implementation of kmp approach for string matching | ||
// Time complexity: O(n+m) | ||
// where n is the length of the text and m is the length of the pattern | ||
// Space complexity: O(m) | ||
|
||
#include<bits/stdc++.h> | ||
using namespace std; | ||
|
||
|
||
void computeLPS(string pattern, vector<int>& lps) | ||
{ | ||
int m = pattern.length(); | ||
int len = 0; | ||
lps[0] = 0; | ||
int i = 1; | ||
|
||
while (i < m) | ||
{ | ||
if (pattern[i] == pattern[len]) | ||
{ | ||
len++; | ||
lps[i] = len; | ||
i++; | ||
} | ||
else | ||
{ | ||
if (len != 0) | ||
{ | ||
len = lps[len - 1]; | ||
} | ||
else | ||
{ | ||
lps[i] = 0; | ||
i++; | ||
} | ||
} | ||
} | ||
} | ||
|
||
vector<int>kmp_approach(string text, string pattern) | ||
{ | ||
int n = text.length(); | ||
int m = pattern.length(); | ||
vector<int> fndIndex; | ||
vector<int> lps(m); | ||
|
||
computeLPS(pattern, lps); | ||
|
||
int i = 0; | ||
int j = 0; | ||
|
||
while ((n-i)>=(m-j)) | ||
{ | ||
if (pattern[j] == text[i]) | ||
{ | ||
i++; | ||
j++; | ||
} | ||
|
||
if (j == m) | ||
{ | ||
fndIndex.push_back(i - j); | ||
cout << "Pattern found at index " << i - j << endl; | ||
j = lps[j - 1]; | ||
} | ||
else if (i < n && pattern[j] != text[i]) | ||
{ | ||
if (j != 0) | ||
{ | ||
j = lps[j - 1]; | ||
} | ||
else | ||
{ | ||
i++; | ||
} | ||
} | ||
} | ||
|
||
return fndIndex; | ||
} | ||
|
||
int main() | ||
{ | ||
string text = "ababcababcabab"; | ||
string pattern = "abc"; | ||
vector<int> fndIndex = kmp_approach(text, pattern); | ||
if (fndIndex.size() == 0) | ||
{ | ||
cout << "Pattern not found" << endl; | ||
} | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// implementation of naive approach for string matching | ||
// Time complexity: O(n*m) | ||
// where n is the length of the text and m is the length of the pattern | ||
// Space complexity: O(1) | ||
|
||
#include<bits/stdc++.h> | ||
using namespace std; | ||
|
||
|
||
vector<int> naive_approach(string text, string pattern) | ||
{ | ||
int n = text.length(); | ||
int m = pattern.length(); | ||
vector<int> fndIndex; | ||
|
||
for (int i = 0; i <= n - m; i++) | ||
{ | ||
int j; | ||
for (j = 0; j < m; j++) | ||
{ | ||
if (text[i + j] != pattern[j]) | ||
{ | ||
break; | ||
} | ||
} | ||
|
||
if (j == m) | ||
{ | ||
fndIndex.push_back(i); | ||
cout << "Pattern found at index " << i << endl; | ||
} | ||
} | ||
|
||
return fndIndex; | ||
} | ||
|
||
int main() | ||
{ | ||
string text = "ababcababcabab"; | ||
string pattern = "abc"; | ||
|
||
vector<int> fndIndex = naive_approach(text, pattern); | ||
|
||
if (fndIndex.size() == 0) | ||
{ | ||
cout << "Pattern not found" << endl; | ||
} | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// implementation of robin karp approach for string matching | ||
// Time complexity: O(n+m) | ||
// where n is the length of the text and m is the length of the pattern | ||
// Space complexity: O(1) | ||
|
||
#include<bits/stdc++.h> | ||
using namespace std; | ||
|
||
#define d 256 | ||
#define q INT_MAX | ||
|
||
vector<int> robin_karp(string text, string pattern) | ||
{ | ||
int n = text.length(); | ||
int m = pattern.length(); | ||
vector<int> fndIndex; | ||
|
||
int h = 1; | ||
for (int i = 0; i < m - 1; i++) | ||
{ | ||
h = (h * d) % q; | ||
} | ||
|
||
int p = 0; | ||
int t = 0; | ||
|
||
for (int i = 0; i < m; i++) | ||
{ | ||
p = (d * p + pattern[i]) % q; | ||
t = (d * t + text[i]) % q; | ||
} | ||
|
||
for (int i = 0; i <= n - m; i++) | ||
{ | ||
if (p == t) | ||
{ | ||
int j; | ||
for (j = 0; j < m; j++) | ||
{ | ||
if (text[i + j] != pattern[j]) | ||
{ | ||
break; | ||
} | ||
} | ||
|
||
if (j == m) | ||
{ | ||
fndIndex.push_back(i); | ||
cout << "Pattern found at index " << i << endl; | ||
} | ||
} | ||
|
||
if (i < n - m) | ||
{ | ||
t = (d * (t - text[i] * h) + text[i + m]) % q; | ||
if (t < 0) | ||
{ | ||
t += q; | ||
} | ||
} | ||
} | ||
|
||
return fndIndex; | ||
} | ||
|
||
int main() | ||
{ | ||
string text = "ababcababcabab"; | ||
string pattern = "abc"; | ||
|
||
vector<int> fndIndex = robin_karp(text, pattern); | ||
|
||
if (fndIndex.size() == 0) | ||
{ | ||
cout << "Pattern not found" << endl; | ||
} | ||
|
||
return 0; | ||
} |