MCPcopy Create free account
hub / github.com/codemistic/Data-Structures-and-Algorithms / KMP

Function KMP

CPP/String/KMPalgo.cpp:33–61  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

31}
32
33void KMP(string txt, string pat)
34{
35 int n = txt.length();
36 int m = pat.length();
37 int lps[m];
38 fillLPS(pat, lps);
39
40 int i = 0, j = 0;
41 while (i < n)
42 {
43 if (txt[i] == pat[j])
44 {
45 i++;
46 j++;
47 }
48 if (j == m)
49 {
50 cout << (i - m) << " ";
51 j = lps[j - 1];
52 }
53 else if (i < n && txt[i] != pat[j])
54 {
55 if (j == 0)
56 i++;
57 else
58 j = lps[j - 1];
59 }
60 }
61}
62
63int main()
64{

Callers 1

mainFunction · 0.85

Calls 2

fillLPSFunction · 0.85
lengthMethod · 0.80

Tested by

no test coverage detected