Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <queue>
- #include <vector>
- using namespace std;
- string pattern, text;
- const int B = 256;
- const int MOD = INT_MAX;
- void robin_carp() {
- int hash_pattern = 0, hash_text = 0;
- for(int i = 0; i < (int) pattern.size(); i++) {
- hash_pattern = (B * hash_pattern + pattern[i]) % MOD;
- hash_text = (B * hash_text + text[i]) % MOD;
- }
- cout << hash_pattern << " " << hash_text << endl;
- int h = 1;
- for(int i = 0; i < (int) pattern.size() - 1; i++) {
- h = (h * B) % MOD;
- }
- for(int i = 0; i <= (int) text.size() - (int) pattern.size(); i++) {
- if(hash_pattern == hash_text) {
- bool ok = true;
- for(int j = 0; j < (int) pattern.size(); j++) {
- if(text[i + j] != pattern[j]) {
- ok = false;
- break;
- }
- }
- if(ok) {
- cout << "found at: " << i << endl;
- }
- }
- if(i < (int) text.size() - (int) pattern.size()) {
- hash_text = (B * (hash_text - text[i] * h) + text[i + (int) pattern.size()]) % MOD;
- if(hash_text < 0) {
- hash_text += MOD;
- }
- }
- }
- }
- int main() {
- ios_base::sync_with_stdio(false);
- cin >> pattern >> text;
- robin_carp();
- return 0;
- }
- /*
- **/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement