0% found this document useful (0 votes)
12 views6 pages

Aec Last

The document presents implementations of two string searching algorithms: the Knuth-Morris-Pratt (KMP) algorithm and the Rabin-Karp (RK) algorithm. The KMP algorithm uses a preprocessing step to create an LPS array for efficient pattern matching, while the RK algorithm uses hashing to compare the pattern and text. Both algorithms include example code and demonstrate their functionality with sample outputs indicating the found pattern indices.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views6 pages

Aec Last

The document presents implementations of two string searching algorithms: the Knuth-Morris-Pratt (KMP) algorithm and the Rabin-Karp (RK) algorithm. The KMP algorithm uses a preprocessing step to create an LPS array for efficient pattern matching, while the RK algorithm uses hashing to compare the pattern and text. Both algorithms include example code and demonstrate their functionality with sample outputs indicating the found pattern indices.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

KMP ALGORITHM:

#include<stdio.h>

#include<string.h>

#include<stdlib.h>

void computeLPSArray(char *pat, int M, int *lps);

void KMPSearch(char *pat, char *txt) {

int M = strlen(pat);

int N = strlen(txt);

// create lps[] that will hold the longest prefix suffix values for pattern

int *lps = (int *) malloc(sizeof(int) * M);

int j = 0; // index for pat[]

// Preprocess the pattern (calculate lps[] array)

computeLPSArray(pat, M, lps);

int i = 0; // index for txt[]

while (i < N) {

if (pat[j] == txt[i]) {

j++;

i++;

if (j == M) {

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

j = lps[j - 1];

// mismatch after j matches


else if (i < N && pat[j] != txt[i]) {

// Do not match lps[0..lps[j-1]] characters,

// they will match anyway

if (j != 0)

j = lps[j - 1];

else

i = i + 1;

free(lps); // to avoid memory leak

void computeLPSArray(char *pat, int M, int *lps) {

int len = 0; // lenght of the previous longest prefix suffix

int i;

lps[0] = 0; // lps[0] is always 0

i = 1;

// the loop calculates lps[i] for i = 1 to M-1

while (i < M) {

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

len++;

lps[i] = len;

i++;

} else // (pat[i] != pat[len])

if (len != 0) {

// This is tricky. Consider the example AAACAAAA and i = 7.

len = lps[len - 1];


// Also, note that we do not increment i here

} else // if (len == 0)

lps[i] = 0;

i++;

// Driver program to test above function

int main() {

char *txt = "ABABDABACDABABCABAB";

char *pat = "ABABCABAB";

KMPSearch(pat, txt);

return 0;

OUTPUT:

Found pattern at index 10

2)RK ALGORITHM

#include<stdio.h>

#include<string.h>

// d is the number of characters in input alphabet

#define d 256

/* pat -> pattern

txt -> text

q -> A prime number

*/
void search(char *pat, char *txt, int q)

int M = strlen(pat);

int N = strlen(txt);

int i, j;

int p = 0; // hash value for pattern

int t = 0; // hash value for txt

int h = 1;

// The value of h would be "pow(d, M-1)%q"

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

h = (h*d)%q;

// Calculate the hash value of pattern and first window of text

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

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

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

// Slide the pattern over text one by one

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

// Check the hash values of current window of text and pattern

// If the hash values match then only check for characters on by one

if ( p == t )

/* Check for characters one by one */

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

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

break;

if (j == M) // if p == t and pat[0...M-1] = txt[i, i+1, ...i+M-1]

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

// Calculate hash value for next window of text: Remove leading digit,

// add trailing digit

if ( i < N-M )

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

// We might get negative value of t, converting it to positive

if(t < 0)

t = (t + q);

/* Driver program to test above function */

int main()

char *txt = "Sanfoundry C Programming Examples";

char *pat = "San";

int q = 101; // A prime number

search(pat, txt, q);

return 0;

}
OUTPUT: Pattern found at index 0

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy