Answers for "creating lps table in kmp algoritm"

C++
0

creating lps table in kmp algoritm

#include<bits/stdc++.h>
using namespace std;
void createlps(int lps[],string pattern,int n)
{
    int i=0;
    int j=1;
    lps[0]=0;
    while(j<n)
    {
        if(pattern[i]==pattern[j])
        {
            lps[j]=i+1;
            i++;
            j++;
        }
        else
        {
            if(i!=0)
            {
                i=lps[i-1];
            }
            else
            {
                lps[j]=0;
                j++;
            }
        }
    }
}
int main()
{
    string pattern;
    cin>>pattern;
    int n=pattern.size();
    int lps[n];
    createlps(lps,pattern,n);
    for(int i=0;i<n;i++)
    {
        cout<<lps[i]<<" ";
    }
}
Posted by: Guest on August-09-2021

Browse Popular Code Answers by Language