-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rabin_Karp.cpp
61 lines (57 loc) · 1 KB
/
Rabin_Karp.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
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <queue>
#include <cmath>
#include <cstring>
#include <stack>
#include <set>
#include <map>
#include <vector>
using namespace std;
#define INF 0x2fffffff
#define LL long long
#define MAX(a,b) ((a)>(b))?(a):(b)
#define MIN(a,b) ((a)<(b))?(a):(b)
#define size 10
#define mod 1000000007
char p[1000];
char t[1000000];
int Rabin_karp(){
int len1 = strlen(p);
int len2 = strlen(t);
int d = 0;
int h = 1;
for(int i = 0;i < len1-1;i++){
h = h*size;
h %= mod;
}
int q = 0;
for(int i = 0;i < len1;i++){
d*=10;d += p[i]-'0';d %= mod;
q*=10;q += t[i]-'0';q %= mod;
}
len2 -= len1;
for(int i = 0;i <= len2;i++){
if(d % mod == q % mod){
int flag = 1;
for(int j = 0;j < len1;j++){
if(t[i+j]!=p[j]){
flag = 0;
break;
}
}
if(flag){
printf("%d ",i+1);
}
}
q = (size*(q-(t[i]-'0')*h) + (t[i+len1]-'0')) % mod;
}
return -1;
}
int main(){
while(cin >> t >> p){
Rabin_karp();
}
return 0;
}