-
Notifications
You must be signed in to change notification settings - Fork 1
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
33e99ac
commit fec9463
Showing
1 changed file
with
59 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,59 @@ | ||
/* | ||
* This program searches for a pattern in a text using | ||
* Rabin - Karp Algorithm | ||
* | ||
* Coded by: Abdurrezak Efe | ||
* | ||
* */ | ||
|
||
|
||
|
||
#include <iostream> | ||
#include <cmath> | ||
#include <vector> | ||
using namespace std; | ||
|
||
int prime = 7; | ||
int pattern_hash; | ||
vector<int> indexes; | ||
|
||
int hasher(string s) | ||
{ | ||
int result = 0; | ||
for(int i=0;i<s.size();i++) | ||
result += (s[i] - 'a' + 1)*pow(prime, s.size()-i-1); | ||
|
||
return result; | ||
} | ||
|
||
void rabin_karp(string text, string pattern) | ||
{ | ||
int m = pattern.size(); | ||
|
||
int cur_hash = hasher(text.substr(0,m)); | ||
|
||
//cout << cur_hash << endl; | ||
|
||
for(int i=0; i < text.size()-m+1; i++) //searching | ||
{ | ||
if(cur_hash == pattern_hash) | ||
if(text.substr(i,m) == pattern) | ||
indexes.push_back(i); | ||
|
||
cur_hash = prime*(cur_hash - (text[i]-'a'+1)*pow(prime, m-1))+(text[i+m]-'a'+1); //updating hash in O(1) | ||
} | ||
} | ||
|
||
|
||
int main() | ||
{ | ||
string text, pattern; | ||
cin >> text >> pattern; | ||
|
||
pattern_hash = hasher(pattern); | ||
rabin_karp(text, pattern); | ||
|
||
for(int i=0;i<indexes.size();i++) | ||
cout << indexes[i] << endl; | ||
|
||
} |