1 of 14

Topic 9: String: Hash, KMP, AC Automata

CS 311 - Competitive Programming II (Fall 2023)

Zhongtang Luo (Instructor)

2 of 14

Code Presentation

3 of 14

Problem… (Subsequences in Substrings)

NAIPC 2019

Not all problems need a technique…

Sample: s=aaaaa, t=aaa

4 of 14

String Search

  • Find some string t in a very long string s
  • Naive complexity O(|t||s|)
  • Goal: make it O(|t|+|s|)

5 of 14

String Search: Rolling Hash

  • Treat string as a number / two numbers

6 of 14

String Search: Rolling Hash

  • Theory
    • Insecure: small modular / non-prime modular / fixed modular & base
    • Secure: fixed prime modular + randomized base (|Z|+1 – p-1)
    • Secure: randomized prime modular (N – 2N) + fixed base
    • See also: Hash Killer
    • See also: https://codeforces.com/blog/entry/60442
  • Practice

7 of 14

Problem… (Palindromes)

There are very famous algorithms that deal with palindromes (Manacher, palindrome tree, etc.)

But for this problem, try to solve it with hash + BSTA.

8 of 14

String Match: Trie?

9 of 14

Problem… (Remember the Word)

10 of 14

String Search: Automata

  • Automata for t=’abcd’
  • Automata for t=’aab’
  • Automata for t=’ababc’

11 of 14

String Search: KMP

  • Optimization on mismatch

12 of 14

String Search: AC Automata

13 of 14

Problem… (Anthem of Berland)

Hint: let dp[i][j] be the case that we are at i-th position in the string and at the j-th position in KMP.

14 of 14

Problem… (DNA repair)