TheAlgorithms/C++
1.0.0
All the algorithms implemented in C++
Toggle main menu visibility
Loading...
Searching...
No Matches
rabin_karp.cpp
Go to the documentation of this file.
1
7
#include <cassert>
8
#include <cmath>
9
#include <iostream>
10
#ifdef _MSC_VER
11
#include <string>
// use this for MS Visucal C++
12
#else
13
#include <cstring>
14
#endif
15
16
#define PRIME 5
17
18
namespace
string_search
{
25
int64_t
create_hash
(
const
std::string& s,
int
n) {
26
int64_t result = 0;
27
for
(
int
i = 0; i < n; ++i) {
28
result += (int64_t)(s[i] * (int64_t)pow(
PRIME
, i));
29
}
30
return
result;
31
}
32
42
int64_t
recalculate_hash
(
const
std::string& s,
int
old_index,
int
new_index,
43
int64_t old_hash,
int
patLength) {
44
int64_t new_hash = old_hash - s[old_index];
45
new_hash /=
PRIME
;
46
new_hash += (int64_t)(s[new_index] * (int64_t)pow(
PRIME
, patLength - 1));
47
return
new_hash;
48
}
49
60
bool
check_if_equal
(
const
std::string& str1,
const
std::string& str2,
61
int
start1,
int
end1,
int
start2,
int
end2) {
62
if
(end1 - start1 != end2 - start2) {
63
return
false
;
64
}
65
while
(start1 <= end1 && start2 <= end2) {
66
if
(str1[start1] != str2[start2]) {
67
return
false
;
68
}
69
start1++;
70
start2++;
71
}
72
return
true
;
73
}
74
82
83
int
rabin_karp
(
const
std::string& str,
const
std::string& pat) {
84
int64_t pat_hash =
create_hash
(pat, pat.size());
85
int64_t str_hash =
create_hash
(str, pat.size());
86
for
(
int
i = 0; i <= str.size() - pat.size(); ++i) {
87
if
(pat_hash == str_hash &&
88
check_if_equal
(str, pat, i, i + pat.size() - 1, 0,
89
pat.size() - 1)) {
90
return
i;
91
}
92
if
(i < str.size() - pat.size()) {
93
str_hash =
94
recalculate_hash
(str, i, i + pat.size(), str_hash, pat.size());
95
}
96
}
97
return
-1;
// return -1 if given pattern not found
98
}
99
100
}
// namespace string_search
101
102
using
string_search::rabin_karp
;
103
105
int
main
(
void
) {
106
assert(
rabin_karp
(
"helloWorld"
,
"world"
) == -1);
107
assert(
rabin_karp
(
"helloWorld"
,
"World"
) == 5);
108
assert(
rabin_karp
(
"this_is_c++"
,
"c++"
) == 8);
109
assert(
rabin_karp
(
"happy_coding"
,
"happy"
) == 0);
110
return
0;
111
}
main
int main()
Main function.
Definition
generate_parentheses.cpp:110
string_search
String search algorithms.
Definition
brute_force_string_searching.cpp:13
string_search::rabin_karp
int rabin_karp(const std::string &str, const std::string &pat)
Definition
rabin_karp.cpp:83
string_search::create_hash
int64_t create_hash(const std::string &s, int n)
Definition
rabin_karp.cpp:25
string_search::check_if_equal
bool check_if_equal(const std::string &str1, const std::string &str2, int start1, int end1, int start2, int end2)
Definition
rabin_karp.cpp:60
string_search::recalculate_hash
int64_t recalculate_hash(const std::string &s, int old_index, int new_index, int64_t old_hash, int patLength)
Definition
rabin_karp.cpp:42
rabin_karp
int rabin_karp(const std::string &str, const std::string &pat)
Definition
rabin_karp.cpp:83
PRIME
#define PRIME
Prime modulus for hash functions.
Definition
rabin_karp.cpp:16
strings
rabin_karp.cpp
Generated by
1.18.0