A robot log reader can receive one continuous symbol sequence in several chunks. If a sought sequence straddles a chunk boundary, searching each chunk independently can miss it even though every symbol arrived. The Knuth-Morris-Pratt algorithm carries the relevant partial-match information forward in a small state. Its fallback links also preserve overlaps after a complete match instead of requiring the reader to restart from an empty state.
Fix the symbols and indexing
This example searches exact ASCII characters, using zero-based positions in the entire input sequence. Letters are invented symbols with no implied robot command or protocol meaning. There is no case folding, wildcard, or approximate matching. These choices make character and byte positions coincide here; other encodings require an explicitly chosen indexing unit.
Let the pattern be P = RTTRT, of length five. Define pi[i] as the length of the longest proper prefix of P[0..i] that is also its suffix. Proper means shorter than the whole prefix under consideration. The empty string is permitted and has length zero.
For the five positions, the table is [0,0,0,1,2]. The prefix RTTR ends with R, matching the pattern's first character. The complete pattern RTTRT ends with RT, matching its first two characters. Neither has a longer proper prefix that is also a suffix.
Cornell's original lecture and Carnegie Mellon's authored string-searching notes explain prefix fallback and its running-time argument. They use different examples and indexing conventions. The pattern, chunk boundaries, state trace, and overlap checks here are independently chosen, with pi indexed by the last character of each prefix throughout.
Store the matched-prefix length
Maintain an integer j giving the number of pattern characters already matched by a suffix of the processed input. Before comparing a new character, j lies between zero and four. The next candidate comparison is with P[j].
On a mismatch while j is positive, replace j by pi[j - 1] and compare the same incoming character again. The fallback retains a shorter suffix already known to match a pattern prefix. If the incoming character then matches P[j], increase j by one. If no candidate prefix can be extended, j stays zero.
Reaching five reports a complete occurrence. Record its starting index as the current absolute input index minus four. For a search that returns every occurrence, immediately replace j by pi[4] = 2. That retains the matching RT suffix so a new occurrence may overlap the one just reported.
Trace a mismatch without moving backward
Use the full text RTTRXRTTRTTRT, whose positions run from zero through twelve. The first four characters advance j through one, two, three, and four. At index four, X differs from the expected P[4] = T.
Fallback takes j from four to pi[3] = one. X also differs from P[1] = T, so j falls to pi[0] = zero. X does not match P[0] = R either. Processing that input character ends with j = zero, and the next character considered is the R at index five.
The same X was compared against several pattern positions, but no earlier input character was fetched again. A left-to-right scan does not mean exactly one comparison per incoming character. Its efficiency depends on bounding the total fallback work across the scan.
Find two overlapping occurrences
From index five, RTTRT completes a match at index nine, starting at five. After reporting it, retain j = two because the already processed text ends in RT. The T at index ten then advances j to three, the R at eleven advances it to four, and the T at twelve completes another occurrence starting at eight.
Thus the two starts are five and eight. Their five-character windows overlap at indices eight and nine. Resetting j to zero after the first match would discard the suffix needed for the second one. An implementation intended to report only nonoverlapping occurrences would be following a different output convention.
Move the same trace across three chunks
Partition the input into RTTRXR, then TTR, then TTRT. After the first chunk, the final R at absolute index five leaves j = one. Processing the second chunk advances that saved state to four, with the processed input ending at index eight.
The first T of the third chunk completes the occurrence starting at five. Continuing with the retained border state finds the occurrence at eight before that chunk ends. Neither occurrence is contained entirely inside one of the three chunks. Resetting the state at each chunk boundary therefore loses both.
For a fixed pattern, the prefix table, current j, and total processed-character count suffice for subsequent matching decisions and absolute offsets. If an application needs to return surrounding text, it must retain that context separately. The compact matching state does not reconstruct discarded log contents.
Why the work stays linear
During matching, j increases by at most one for each consumed input character. Every fallback strictly decreases it, and j never becomes negative. Total decreases are therefore bounded by the accumulated increases. Building the prefix table uses an analogous argument over the pattern itself. The resulting total time is O(n + m), with O(m) pattern-related storage for text length n and pattern length m.
That bound assumes constant-time comparisons of the chosen symbols. It does not include an unrelated decoder's or tokenizer's cost, and it is not a measured speed claim for a particular log-processing system.
Preserve the intended stream semantics
The example uses a nonempty pattern; an empty-pattern convention needs its own definition. Retaining state across independent records may also be wrong if matches must never cross record boundaries. Reset at a semantic boundary when the specification requires it, rather than treating every transport chunk as a new record.
KMP establishes exact symbol-sequence occurrence. It does not parse message structure or authenticate a command. Keep the pattern identity, prefix-table convention, stream offset, and boundary policy with the result so an apparent match can be checked against the actual question being asked.
Sources checked October 24, 2026:
https://www.cs.cornell.edu/courses/cs312/2002sp/lectures/lec25.htm
https://www.cs.cmu.edu/~cdm/resources/36-string-match.pdf
Concept illustration: A small desktop robot beside a laptop showing a generic workflow provides robotics context for this analytical model. The image does not depict the article's numerical example, a verified experiment, a product specification, or deployment evidence.