//
// main.cpp
// KMP Algorithm
//
// Created by Himanshu on 31/05/23.
//
#include <iostream>
#include <vector>
#include <string>
using namespace std;
void printVector ( vector< int > vec) {
for ( int x : vec) {
cout << x<< " " ;
}
cout << endl;
}
vector< int > constructLPS ( string pattern) {
int n = ( int ) pattern.length ( ) ;
vector< int > lps( n, 0 ) ;
int j = 0 ;
for ( int i = 1 ; i < n; i++ ) {
if ( pattern[ i] == pattern[ j] ) {
j++ ;
} else {
while ( j > 0 && pattern[ i] ! = pattern[ j] ) {
j = lps[ j- 1 ] ;
}
}
lps[ i] = j;
}
return lps;
}
void kmpStringSearch ( string text, string pattern) {
int n = ( int ) text.length ( ) ;
int m = ( int ) pattern.length ( ) ;
vector< int > lps = constructLPS( pattern) ;
int i = 0 ;
int j = 0 ;
int count = 0 ;
while ( i < n) {
cout << endl<< "Iteration " << ++ count<< ":" << endl;
cout << "i = " << i<< ", text[i] = " << text[ i] << endl;
cout << "j = " << j<< ", pattern[j] = " << pattern[ j] << endl;
if ( text[ i] == pattern[ j] ) {
i++ ;
j++ ;
}
if ( j == m) {
cout << endl<< "Pattern found at index " << i - j<< endl;
j = lps[ j- 1 ] ;
} else if ( i < n && text[ i] ! = pattern[ j] ) {
if ( j ! = 0 ) {
j = lps[ j- 1 ] ;
} else {
i++ ;
}
}
}
}
int main( ) {
string text = "BABABA" ;
string pattern = "ABA" ;
kmpStringSearch( text, pattern) ;
return 0 ;
}
Ly8KLy8gIG1haW4uY3BwCi8vICBLTVAgQWxnb3JpdGhtCi8vCi8vICBDcmVhdGVkIGJ5IEhpbWFuc2h1IG9uIDMxLzA1LzIzLgovLwoKI2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dmVjdG9yPgojaW5jbHVkZSA8c3RyaW5nPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKdm9pZCBwcmludFZlY3RvciAodmVjdG9yPGludD4gdmVjKSB7CiAgICBmb3IgKGludCB4IDogdmVjKSB7CiAgICAgICAgY291dDw8eDw8IiAiOwogICAgfQogICAgY291dDw8ZW5kbDsKfQoKdmVjdG9yPGludD4gY29uc3RydWN0TFBTIChzdHJpbmcgcGF0dGVybikgewogICAgaW50IG4gPSAoaW50KSBwYXR0ZXJuLmxlbmd0aCgpOwogICAgdmVjdG9yPGludD4gbHBzKG4sIDApOwogICAgaW50IGogPSAwOwoKICAgIGZvciAoaW50IGkgPSAxOyBpIDwgbjsgaSsrKSB7CiAgICAgICAgCiAgICAgICAgaWYgKHBhdHRlcm5baV0gPT0gcGF0dGVybltqXSkgewogICAgICAgICAgICBqKys7CiAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgd2hpbGUgKGogPiAwICYmIHBhdHRlcm5baV0gIT0gcGF0dGVybltqXSkgewogICAgICAgICAgICAgICAgaiA9IGxwc1tqLTFdOwogICAgICAgICAgICB9CiAgICAgICAgIAogICAgICAgIH0KICAgICAgICAKICAgICAgICBscHNbaV0gPSBqOwogICAgfQoKICAgIHJldHVybiBscHM7Cn0KCnZvaWQga21wU3RyaW5nU2VhcmNoIChzdHJpbmcgdGV4dCwgc3RyaW5nIHBhdHRlcm4pIHsKICAgIGludCBuID0gKGludCkgdGV4dC5sZW5ndGgoKTsKICAgIGludCBtID0gKGludCkgcGF0dGVybi5sZW5ndGgoKTsKICAgIHZlY3RvcjxpbnQ+IGxwcyA9IGNvbnN0cnVjdExQUyhwYXR0ZXJuKTsKICAgIAogICAgaW50IGkgPSAwOwogICAgaW50IGogPSAwOwogICAgaW50IGNvdW50ID0gMDsKCiAgICB3aGlsZSAoaSA8IG4pIHsKICAgICAgICAKICAgICAgICBjb3V0PDxlbmRsPDwiSXRlcmF0aW9uICI8PCsrY291bnQ8PCI6Ijw8ZW5kbDsKICAgICAgICBjb3V0PDwiaSA9ICI8PGk8PCIsIHRleHRbaV0gPSAiPDx0ZXh0W2ldPDxlbmRsOwogICAgICAgIGNvdXQ8PCJqID0gIjw8ajw8IiwgcGF0dGVybltqXSA9ICI8PHBhdHRlcm5bal08PGVuZGw7CiAgICAgICAgCiAgICAgICAgaWYgKHRleHRbaV0gPT0gcGF0dGVybltqXSkgewogICAgICAgICAgICBpKys7CiAgICAgICAgICAgIGorKzsKICAgICAgICB9CgogICAgICAgIGlmIChqID09IG0pIHsKICAgICAgICAgICAgY291dDw8ZW5kbDw8IlBhdHRlcm4gZm91bmQgYXQgaW5kZXggIjw8IGkgLSBqPDxlbmRsOwogICAgICAgICAgICBqID0gbHBzW2otMV07CiAgICAgICAgfSBlbHNlIGlmIChpIDwgbiAmJiB0ZXh0W2ldICE9IHBhdHRlcm5bal0pIHsKICAgICAgICAgICAgCiAgICAgICAgICAgIGlmIChqICE9IDApIHsKICAgICAgICAgICAgICAgIGogPSBscHNbai0xXTsKICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICAgIGkrKzsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgICAgICAKICAgIH0KICAgIAp9CgppbnQgbWFpbigpIHsKICAgIHN0cmluZyB0ZXh0ID0gIkJBQkFCQSI7CiAgICBzdHJpbmcgcGF0dGVybiA9ICJBQkEiOwoKICAgIGttcFN0cmluZ1NlYXJjaCh0ZXh0LCBwYXR0ZXJuKTsKCiAgICByZXR1cm4gMDsKfQ==
stdout
Iteration 1:
i = 0, text[i] = B
j = 0, pattern[j] = A
Iteration 2:
i = 1, text[i] = A
j = 0, pattern[j] = A
Iteration 3:
i = 2, text[i] = B
j = 1, pattern[j] = B
Iteration 4:
i = 3, text[i] = A
j = 2, pattern[j] = A
Pattern found at index 1
Iteration 5:
i = 4, text[i] = B
j = 1, pattern[j] = B
Iteration 6:
i = 5, text[i] = A
j = 2, pattern[j] = A
Pattern found at index 3