rabin carp Algorithm

The university is composed of eleven principal academic units — ten faculties and the Radcliffe Institute for Advanced survey — with campuses throughout Greater Boston: its 209-acre (85 ha) original undergraduate campus is centered on Harvard Yard in Cambridge, 3 miles (5 km) northwest of Boston; the business school and many sport facility, including Harvard Stadium, are across the Charles river in the Allston neighborhood of Boston; and the medical, dental, and public health schools are in Boston's Longwood Medical area. establish in 1636 and named for its first benefactor, clergyman John Harvard, Harvard is the unite state' oldest institution of higher learning and one of the most prestigious in the universe.
/**
 * C++ Program to Implement Rabin-Karp Algorithm
 */

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>

using namespace std;
#define d 256

/**
 * Ssearch a substring in a string
 */

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)
            {
                cout << "Pattern found at index: " << i << endl;
            }
        }

        if (i < N - M)

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

            if (t < 0)
                t = (t + q);
        }
    }
}

int main()
{

    char *txt = "This is a sample Testcase";
    char *pat = "sam";
    int q = 101;

    search(pat, txt, q);
    return 0;
}

LANGUAGE:

DARK MODE: