forked from gghati/Standard-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 14
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
5dca6eb
commit 6656b77
Showing
3 changed files
with
107 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
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,72 @@ | ||
/* | ||
Rabin-Karp Algorithm: | ||
source : https://cp-algorithms.com/string/rabin-karp.html | ||
*/ | ||
|
||
#include <bits/stdc++.h> | ||
using namespace std; | ||
#define int long long | ||
|
||
const int BASE = 31, MOD = 1e9 + 7, N = 1e6 + 5; | ||
|
||
vector<int> pp(N); | ||
|
||
void pre() | ||
{ | ||
pp[0] = 1; | ||
for (int i = 1; i < N; i++) | ||
pp[i] = pp[i - 1] * BASE % MOD; | ||
} | ||
|
||
vector<int> rabin_karp(const string &text, const string &pat) | ||
{ | ||
int tn = text.size(), pn = pat.size(); | ||
vector<int> pref(tn + 1, 0); | ||
for (int i = 0; i < tn; i++) | ||
pref[i + 1] = (pref[i] + (text[i] - 'a' + 1) * pp[i]) % MOD; | ||
int hs = 0; | ||
for (int i = 0; i < pn; i++) | ||
hs = (hs + (pat[i] - 'a' + 1) * pp[i]) % MOD; | ||
|
||
vector<int> res; | ||
|
||
//checking all substrings of length pattern string | ||
for (int i = 0; i + pn - 1 < tn; i++) | ||
{ | ||
int curr = (pref[i + pn] - pref[i] + MOD) % MOD; | ||
if (curr == hs * pp[i] % MOD) | ||
res.push_back(i); | ||
} | ||
return res; | ||
} | ||
|
||
signed main() | ||
{ | ||
ios::sync_with_stdio(0); | ||
cin.tie(0); | ||
cout.tie(0); | ||
|
||
pre(); | ||
|
||
int T = 1; | ||
cin >> T; | ||
while (T--) | ||
{ | ||
string text, pat; | ||
cin >> text >> pat; | ||
|
||
auto res = rabin_karp(text, pat); | ||
if (res.size() == 0) | ||
cout << "Not Found\n"; | ||
else | ||
{ | ||
cout << res.size() << '\n'; | ||
for (auto &&x : res) | ||
cout << ++x << ' '; | ||
cout << '\n'; | ||
} | ||
cout << '\n'; | ||
} | ||
|
||
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,32 @@ | ||
//Source: https://cp-algorithms.com/string/z-function.html | ||
|
||
//basically z[i] = k, means first k characters of string concide with k characters starting from s[i]; | ||
|
||
/* | ||
Example :- | ||
"abacaba" - [0, 0, 1, 0, 3, 0, 1] | ||
here z[4] = 3; //0 based indexing | ||
means first 3 characters from s[4] = first 3 character from s[0]; | ||
*/ | ||
|
||
vector<int> z_function(string &s) | ||
{ | ||
int n = s.size(); | ||
vector<int> z(n); | ||
for (int i = 1, l = 0, r = 0; i < n; i++) | ||
{ | ||
if (i <= r) | ||
z[i] = min(r - i + 1, z[i - l]); | ||
while (i + z[i] < n && s[z[i]] == s[i + z[i]]) | ||
z[i]++; | ||
if (i + z[i] - 1 > r) | ||
l = i, r = i + z[i] - 1; | ||
} | ||
return z; | ||
} | ||
|
||
/* | ||
Example | ||
Problem : https://codeforces.com/contest/432/problem/D | ||
solution : https://codeforces.com/contest/432/submission/75147854 | ||
*/ |