English

Scan a Pattern with Prefix Fallback

Reuse matched prefixes when an exact string scan encounters a mismatch.

Adult planning suggestion: about 9–12 · Logic & rule checking

Players
2–4; cooperate, then exchange the checker role
Time
15–25 minutes
Materials
Paper, pencil and the supplied player sheet; keep the host sheet separate.

Prepare

Use text ABABABAC and pattern ABABAC with zero-based indices. Keep text position i and matched-prefix length j. First build the pattern prefix table pi, where pi[i] is its longest proper prefix also ending at i.

Follow the rule

To build pi, start pi[0]=0. At each later i set j=pi[i−1]; while j>0 and symbols differ, set j=pi[j−1]. If symbols now match, increase j by 1, then write pi[i]=j.

Check each step

Scan the text once. On a mismatch use the same fallback pi[j−1] until comparison can resume, without moving the text pointer backwards. Matching increases j. A full match starts at i−pattern length+1; then reset j to pi[j−1].

Worked example

At text index 5 the B mismatches the expected C after matching ABABA. Reuse the shorter border rather than restarting the text. Write the compared indices beside each fallback.

Finish and adapt

Finish by comparing every reported match with direct substring checking. An empty pattern is outside this kit. The activity checks exact matching, not edit distance, spelling correction or a software speed benchmark.

Purpose, observation and provenance

Within this activity: Reuse matched prefixes when an exact string scan encounters a mismatch.

Ask the checker to name a particular rule and show the recorded evidence. Speed alone is not the goal.

Our rules, examples and printable diagrams are separately prepared. The earliest inventor of the broader mechanism is not established here.

CP Algorithms · Prefix Function — Mechanism background only. Our small paper instance and role rotation are separate; no endorsement or unique-solution claim.

Secondary practice: Cooperation and explaining a check.

For a class of 30, use ten groups of three. Prepare one player-material set per group and keep the host key with the adult. Rotate operator, recorder and checker after each worked example; a reader or scribe may help without choosing answers. Avoid public ranking or diagnostic labels.

Age bands are planning suggestions. Background references may address older learners and do not validate suitability for this age band. Goals describe observable actions, not measured long-term gains or institutional endorsement.

Free alternative: Copy the supplied grids, labels and cards onto reused paper. No specialist product is needed. Adults may prepare pieces or act as a scribe.

Printable materials

Preview at 100% scale, with browser headers and footers off. Cut with suitable help. Keep host keys separate. This is a manual tabletop kit, not an online opponent, automatic judge or solver.

Scan a Pattern with Prefix Fallback · Player materials

Use text ABABABAC and pattern ABABAC with zero-based indices. Keep text position i and matched-prefix length j. First build the pattern prefix table pi, where pi[i] is its longest proper prefix also ending at i.

To build pi, start pi[0]=0. At each later i set j=pi[i−1]; while j>0 and symbols differ, set j=pi[j−1]. If symbols now match, increase j by 1, then write pi[i]=j.

Scan the text once. On a mismatch use the same fallback pi[j−1] until comparison can resume, without moving the text pointer backwards. Matching increases j. A full match starts at i−pattern length+1; then reset j to pi[j−1].

Text / 文本Pattern / 模式
ABABABACABABAC
Pattern index / 模式索引0 1 2 3 4 5
Step / cellDecision / valueRule checked
________________________
________________________
________________________
________________________

Scan a Pattern with Prefix Fallback · Host key — keep separate

pi=0,0,1,2,3,0. At text5 fallback j5→3 then match B, j4. Subsequent A,C complete at text7; start2. Direct text[2:8]=ABABAC.

Lead and check

Use text ABABABAC and pattern ABABAC with zero-based indices. Keep text position i and matched-prefix length j. First build the pattern prefix table pi, where pi[i] is its longest proper prefix also ending at i.

To build pi, start pi[0]=0. At each later i set j=pi[i−1]; while j>0 and symbols differ, set j=pi[j−1]. If symbols now match, increase j by 1, then write pi[i]=j.

Scan the text once. On a mismatch use the same fallback pi[j−1] until comparison can resume, without moving the text pointer backwards. Matching increases j. A full match starts at i−pattern length+1; then reset j to pi[j−1].

At text index 5 the B mismatches the expected C after matching ABABA. Reuse the shorter border rather than restarting the text. Write the compared indices beside each fallback.

Finish by comparing every reported match with direct substring checking. An empty pattern is outside this kit. The activity checks exact matching, not edit distance, spelling correction or a software speed benchmark.

Classroom: ten trios for 30 learners; one kit per trio. Rotate operator, recorder and checker. Age guidance is a planning suggestion; prerequisite support may be necessary.

Related activities

Activity library · Minimum symbol edits