String Matching Algorithms in C++: KMP Prefix Function Examples

Computer science1.57 KB

Summary

This document presents several C++ programs that use string matching techniques, mainly the KMP prefix function. The examples cover finding a pattern in a cyclic string, counting pattern occurrences up to a limit, finding the longest matching words, computing the minimal period of a string, locating all occurrences of one string in another, and counting palindromic-like borders with the Z-function. The code is provided without explanations or comments.

Key points

  • The prefix function p[i] stores the length of the longest proper prefix of a substring that is also its suffix.
  • The KMP algorithm finds a pattern in a text in linear time by reusing the prefix function when a mismatch occurs.
  • A string of length n has minimal period n - p[n-1], where p is the prefix function.
  • The Z-function z[i] gives the length of the longest common prefix between the string and its suffix starting at i.
  • Programs often process cyclic shifts by using indices modulo n, so the text length is extended to 2n or more.

Test yourself

C++ String Matching Algorithms

The following programs implement string matching techniques in C++, mainly the KMP prefix function.

1. Pattern Search in a Cyclic String

This program checks whether a pattern appears in a cyclic version of a string, using the prefix function and indices modulo n.

2. Counting Pattern Occurrences

This program counts how many times a pattern s appears in a text t and compares the count with a given value k, printing "YES" or "NO".

3. Finding Cyclic Shifts

This program searches for one string inside the cyclic extension of another and prints the first position where it occurs.

4. Longest Matching Words

This program converts strings to lowercase, computes the prefix function of each word combined with a pattern, and prints the words with the longest match.

5. Minimal Period of a String

This program computes the length of the string s repeated k times, based on the smallest period n - p[n-1].

6. Finding All Occurrences

This program finds all positions where the shorter string occurs inside the longer one and prints the count and the positions.

7. Minimal Period Calculation

This program prints the smallest period of a string, computed as n - p[n-1].

8. Z-Function Border Count

This program uses the Z-function to count the positions L where the prefix of length L occurs again in the string, with 2L < n.

Download

PDF Word HTML

FAQ

What is the prefix function used for in these programs?

The prefix function is used to find pattern matches efficiently in KMP-style string searching. It also gives the minimal period of a string through n - p[n-1].

How does the code find cyclic shifts of a string?

It iterates over the text using indices modulo n, such as b[i % n], so the string is treated as repeating. This allows matches that wrap around the end of the string.

What does the Z-function compute in the last program?

The Z-function gives, for each position, the length of the longest substring starting there that matches the prefix of the string. The program uses it to count specific border-like positions.

Test yourself

Flashcards

Click a card to see the answer.

Quiz

1. What does the prefix function p[i] represent?
2. What is the minimal period of a string s of length n according to the prefix function?
3. Why does the cyclic shift code use b[i % n]?
4. Which algorithm does the last program use to compute the array z?

Questions generated by AI from the document: check the answers against the text.