forked from hmm34/narq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tools.cpp
85 lines (73 loc) · 2 KB
/
tools.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
76
77
78
79
80
81
82
83
84
85
/* @copydoc tools.hpp */
#include "tools.hpp"
#include <iostream>
#include <map>
#include <algorithm>
namespace narq
{
const unsigned int BASE = 257; // reduces collision in hash function
const unsigned int MOD = 1000000007; // number of elements in hash table
// --------------------------------------------------------------------- //
std::vector<int> rabinKarpMulti(std::vector<std::string>& needles, std::string haystack, int numNeedles)
{
// Initialize to 0 matches for each needle
std::vector<int> matches(numNeedles);
for (int i = 0; i < numNeedles; ++i)
matches.push_back(0);
long long hn[numNeedles];
long long haystackHash = 0;
long long power = 1;
// Compute hash for each needle
size_t m = needles[0].size();
for (int i = 0; i < numNeedles; ++i) {
hn[i] = rhash(needles[i]);
}
for (size_t i = 0; i < needles[0].size(); ++i)
power = (power * BASE) % MOD; // power = BASE ^ (size of string to search for)
for (size_t i = 0; i < haystack.size(); ++i)
{
// Calculate the rolling hash for the haystack
haystackHash = haystackHash * BASE + haystack[i];
haystackHash = haystackHash % MOD;
// "Skip", or remove, previous strings from the haystack
if (i >= m - 1)
{
if (i >= m)
{
haystackHash -= power * haystack[i - m] % MOD;
if (haystackHash < 0)
haystackHash += MOD;
}
// Check if hashes are equal
for (int k = 0; k < numNeedles; ++k)
{
if (hn[k] == haystackHash)
matches[k]++;
}
}
}
return matches;
}
unsigned long long rhash(const std::string & s)
{
long long ret = 0;
for (size_t i = 0; i < s.size(); ++i)
{
ret = ret * BASE + s[i];
ret = ret % MOD;
}
return ret;
}
std::vector<std::string> partition(std::string &s, int numPartitions)
{
std::vector<std::string> needles;
int m = s.size();
int n = m / numPartitions;
for (int i = 0; (i+n) <= m; i += n)
{
std::string needle = s.substr(i, n);
needles.push_back(needle);
}
return needles;
}
}