-
Notifications
You must be signed in to change notification settings - Fork 368
/
RabinKarpAlgo.c
91 lines (75 loc) · 2.2 KB
/
RabinKarpAlgo.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
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
86
87
88
89
90
91
// =========================== Problem Statement ==============================================================
/*
Rabin-Karp algorithm is an algorithm used for searching/matching patterns in the text using a hash function.
Unlike Naive string matching algorithm, it does not travel through every character in the initial phase rather
it filters the characters that do not match and then performs the comparison.
Rabin-Karp Algo using C
Example:-
Input:
Text: "AABAACAADAABAABA"
Pattern: "AABA"
Output:
Occurrences found at indices: 0, 9, 12
*/
#include <stdio.h>
#include <string.h>
// in order to prevent spurious hits
#define d 5
void rabinKarp(char pattern[], char text[], int q)
{
int sizep = strlen(pattern);
int sizet = strlen(text);
int p = 0, t = 0, h = 1;
// h=d^(sizep-1) to calculate the rolling hash
for (int i = 0; i < sizep - 1; i++)
{
h = (h * d) % q;
}
// calculating the hash values of pattern and portions of text
for (int i = 0; i < sizep; i++)
{
p = (d * p + pattern[i]) % q;
t = (d * t + text[i]) % q;
}
// checking for the presence of pattern within the text
printf("Occurences found at indices : ");
int check = 0;
for (int i = 0; i <= sizet - sizep; i++)
{
if (p == t)
{
int j = 0;
while (j < sizep)
{
// checking for spurious hits
if (text[i + j] != pattern[j])
break;
j++;
}
// printing the index of starting of pattern within text
if (j == sizep)
{
printf("%d ", i);
check = 1;
}
}
// calculating the rolling hash for next set of characters
if (i < sizet - sizep)
{
t = (d * (t - text[i] * h) + text[i + sizep]) % q;
if (t < 0)
t += q;
}
}
if (check == 0)
printf("NULL");
}
void main()
{
// inputs:
char pattern[]="AABAACAADAABAABA";
char text[]="AABA";
// taking q as a large prime number to prevent hash value from becoming too large a value
int q = 19;
rabinKarp(pattern, text, q);
}