-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpBq2.c
45 lines (38 loc) · 860 Bytes
/
pBq2.c
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
#include <stdio.h>
#include <string.h>
#include "math.h"
// d is the number of characters in the input alphabet
#define d 256
void rabinkarp(char pattern[], char text[])
{
int m = strlen(pattern);
int n = strlen(text);
int p = 0, t = 0; // hash value
int q = 11;
int units = pow(10, m-1);
for (int c=0; c<m; c++)
{
p = p + pattern[c]*units;
t = t + text[c]*units;
units = units/10;
}
int x;
for (int c=0; c<(n-m); c++)
{
if (p%q==t%q)
{
if (p == t)
printf("Match found at index: %d", c);
}
x = t-((pow(10, m-1))*text[c]);
t = 10*x;
t = t + text[c+m];
}
}
int main()
{
char text[] = "3141592653589793";
char pattern[] = "26";
rabinkarp(pattern, text);
return 0;
}