Knuth-Morris-Pratt Algorithm

Is the solution for the Knut-Morris-Pratt algorithm : Haystack: AAAAAAAAAA, needle: AAA, which is: 3, right?

Because there are 8 AAA instances in the haystack, however, as far as I understand, the knuth-morris-pratt algorithm will only find 3. Am I mistaken about this?

Is it possible to solve this problem by knowing the boundaries for each suffix in a string?

The following is my implementation of the KMP algorithm:

public static int occurrenceOfSubstring(char[] target, char[] pattern) {
        int[] overlay = new int[pattern.length];
        overlay[0] = -1;
        overlay[1] = 0;

        int i = 0, j = 1;

        while (j + 1 < pattern.length) {
            if (pattern[i] == pattern[j]) {
                if (i == 0) {
                    overlay[j + 1] = 1;
                } else {
                    overlay[j + 1] = overlay[j] + 1;
                }
                i++;
                j++;
            } else if (pattern[j] == pattern[0]) {
                i = 0;
            } else {
                j++;
            }
        }

        int l = 0,count=0;

        for (int k = 0; k < target.length; k++) {
            if (target[k] == pattern[l]) {
                if (l == pattern.length - 1) {
                    l = 0;
                    count++;
                } else {
                    l++;
                }
            } else {
                l = overlay[l] == -1 ? 0 : overlay[l];
            }
        }
        return count;
    }
+4
source share
1 answer

KMP , , , . , , , . , KMP 3 . , , , , KMP. , .

+1

Source: https://habr.com/ru/post/1524309/


All Articles