Search
 
SCRIPT & CODE EXAMPLE
 

CPP

kmp c++

#include <string>
#include <iostream>
#include <vector>
using namespace std;
int main()
{
    string l;
    string s;
    cin >> l;
    cin >> s;
    vector<long long>v(s.size(),0);
    int i, j;
    i = 0;
    j = 0;

    for (int i = 0; i < s.size(); i++) {
        i++;
        if (s[j] == s[i]) {
            v[i] = v[i - 1] + 1;
            j++;
        }
        else {
            if (j == 0) {
                j = v[j ];
            }
            else {
                j = v[j - 1];
            }
        }
    }
    j = 0;
    for (int i = 0; i < l.size(); i++) {
        if (l[i] == s[j]) {
            j++;
            if (j = s.size()) {
                cout << "start= " << i - s.size() << " end= " << i ;
                i = l.size();
            }

        }
        else {
            if (j == 0) {
                j = 0;
            }
            else {
                j = v[j - 1];
            }
        }
    }
}
Comment

kmp algorithm c++

#define ll long long int
#define vll vector<long long int>

ll kmp(string s)
{
    ll n = s.size();
    vll lps(n, 0); // longest prefix suffix
    for (ll i = 1; i < n; i++)
    {
        ll j = lps[i - 1];
        while (j > 0 && s[i] != s[j])
        {
            j = lps[j - 1];
        }
        if (s[i] == s[j])
        {
            j++;
        }
        lps[i] = j;
    }
    return lps[n - 1];
}
Comment

kmp c++

void computeLPSArray(char* pat, int M, int* lps)
{
    int len = 0;
    lps[0] = 0;
    int i = 1;
    while (i < M) {
        if (pat[i] == pat[len]) {
            len++;
            lps[i] = len;
            i++;
        }
        else
        {
            if (len != 0) {
                len = lps[len - 1];
            }
            else
            {
                lps[i] = 0;
                i++;
            }
        }
    }
}
int matchFound=0;
void KMPSearch(char* pat, char* txt)
{
    matchFound=0;
    int M = strlen(pat);
    int N = strlen(txt);
    int lps[M];
    computeLPSArray(pat, M, lps);
    int i = 0;
    int j = 0;
    while (i < N) {
        if (pat[j] == txt[i]) {
            j++;
            i++;
        }
        if (j == M) {
            matchFound++;
//            printf("Found pattern at index %d ", i - j);
            j = lps[j - 1];
        }
        else if (i < N && pat[j] != txt[i]) {
            if (j != 0)
                j = lps[j - 1];
            else
                i = i + 1;
        }
    }
}
Comment

PREVIOUS NEXT
Code Example
Cpp :: c++ constructor call superclass 
Cpp :: prime number c++ 
Cpp :: overload array operator cpp 
Cpp :: exponent power of x using c c++ 
Cpp :: looping in map c++ 
Cpp :: how to declare a vector of int in c++ 
Cpp :: toString method in c++ using sstream 
Cpp :: cyclic array rotation in cpp 
Cpp :: trie code cpp 
Cpp :: User defined functions and variables in C++ programming 
Cpp :: oncomponentendoverlap ue4 c++ 
Cpp :: Lambda capture as const cpp 
Cpp :: c++ get data type 
Cpp :: initialize 2d vector c++ 
Cpp :: rethrow exception c++ 
Cpp :: c++ define constant in class header 
Cpp :: variadic template in c++ 
Cpp :: flag of georgia 
Cpp :: c++ generic pointer 
Cpp :: c++ multiline string 
Cpp :: runtime 
Cpp :: custom slider cpt wordpress theme 
Cpp :: How to generate all the possible subsets of a set ? 
Cpp :: c++ power of two 
Cpp :: c++ itoa 
Cpp :: sstream c++ 
Cpp :: qt c++ qdockwidget remove title 
Cpp :: Array declaration by specifying the size and initializing elements in C++ 
Cpp :: How do you count the occurrence of a given character in a string? c++ 
Cpp :: c++ write number to registry 
ADD CONTENT
Topic
Content
Source link
Name
1+5 =