Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CPP

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];
}
 
PREVIOUS NEXT
Tagged: #kmp #algorithm
ADD COMMENT
Topic
Name
5+6 =