0% found this document useful (0 votes)
57 views

Name - Aryan Gupta Reg. No. 199301088 Section - B: Ans.1) A. Rabin Karp String Matching Algorithm Code

The document contains code for two string matching algorithms: 1) The Rabin Karp algorithm, which searches for a pattern in a text by computing a hash of the pattern and sliding window of text and comparing hashes. 2) The KMP algorithm, which uses a prefix table to skip matching characters if a mismatch occurs by finding the longest prefix that is also a suffix. The code implements the prefix table construction and KMP search.

Uploaded by

bruh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views

Name - Aryan Gupta Reg. No. 199301088 Section - B: Ans.1) A. Rabin Karp String Matching Algorithm Code

The document contains code for two string matching algorithms: 1) The Rabin Karp algorithm, which searches for a pattern in a text by computing a hash of the pattern and sliding window of text and comparing hashes. 2) The KMP algorithm, which uses a prefix table to skip matching characters if a mismatch occurs by finding the longest prefix that is also a suffix. The code implements the prefix table construction and KMP search.

Uploaded by

bruh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

NAME – ARYAN

GUPTA
Reg. No. 199301088
SECTION – B

Ans.1)
a. Rabin Karp String matching algorithm

CODE –

#include<stdio.h>

#include<string.h>

#define d 256

void search(char pat[], char txt[], int q)

int M = strlen(pat);

int N = strlen(txt);

int i, j;

int p = 0;

int t = 0;

int h = 1;

for (i = 0; i < M-1; i++)

h = (h*d)%q;

for (i = 0; i < M; i++)

p = (d*p + pat[i])%q;

t = (d*t + txt[i])%q;
}

for (i = 0; i <= N - M; i++)

if ( p == t )

for (j = 0; j < M; j++)

if (txt[i+j] != pat[j])

break;

if (j == M)

printf("Pattern found at index %d \n", i);

if ( i < N-M )

t = (d*(t - txt[i]*h) + txt[i+M])%q;

if (t < 0)

t = (t + q);

int main()

char txt[] = "MANIPAL UNIVERSITY JAIPUR";

char pat[] = "JAIPUR";

int q = 101;
search(pat, txt, q);

return 0;

OUTPUT –
b. KMP Matcher

CODE –

#include<iostream>

#include<string.h>

using namespace std;

void prefixSuffixArray(char* pat, int M, int* pps) {

int length = 0;

pps[0] = 0;

int i = 1;

while (i < M) {

if (pat[i] == pat[length]) {

length++;

pps[i] = length;

i++;

} else {

if (length != 0)

length = pps[length - 1];

else {

pps[i] = 0;

i++;

void KMPAlgorithm(char* text, char* pattern) {


int M = strlen(pattern);

int N = strlen(text);

int pps[M];

prefixSuffixArray(pattern, M, pps);

int i = 0;

int j = 0;

while (i < N) {

if (pattern[j] == text[i]) {

j++;

i++;

if (j == M) {

printf("Found pattern at index %d\n", i - j);

j = pps[j - 1];

else if (i < N && pattern[j] != text[i]) {

if (j != 0)

j = pps[j - 1];

else

i = i + 1;

int main() {

char text[] = "MANIPAL UNIVERSITY JAIPUR";

char pattern[] = "SITY";

printf("The pattern is found in the text at the following index : \n");

KMPAlgorithm(text, pattern);

return 0;

}
OUTPUT -

You might also like