Sequence Automaton
Before reading this article, please read Automaton.
Definition
A sequence automaton is an automaton that accepts exactly the subsequences of a given string.
In this article, \(s\) refers to this string.
States
If \(s\) contains \(n\) characters, then the sequence automaton contains \(n+1\) states.
Let \(t\) be a subsequence of \(s\). Then \(\delta(start, t)\) is the position of the end of the first occurrence of \(t\) in \(s\).
That is, a state \(i\) represents the set difference between the subsequences of the prefix \(s[1..i]\) and the subsequences of the prefix \(s[1..i-1]\).
All states in the sequence automaton are accepting states.
Transitions
From the state definition, we have \(\delta(u, c)=\min\{i|i>u,s[i]=c\}\), which is the position of the next occurrence of character \(c\).
Why is it the "next" occurrence? Because if \(i>j\), the set of subsequences of the suffix \(s[i..|s|]\) is a subset of the subsequences of the suffix \(s[j..|s|]\). Thus, choosing the earliest possible occurrence is always optimal.
Implementation
Scan from back to front, maintaining the earliest occurrence position of each character:
The construction complexity is \(O(n|\Sigma|)\).
Example Problems
「HEOI2015」Shortest Non-Common Substring
You are given two strings \(A\) and \(B\) consisting of lowercase English letters (\(1\le |A|, |B|\le 2000\)). Find:
- The shortest substring of \(A\) that is not a substring of \(B\);
- The shortest substring of \(A\) that is not a subsequence of \(B\);
- The shortest subsequence of \(A\) that is not a substring of \(B\);
- The shortest subsequence of \(A\) that is not a subsequence of \(B\).
Solution
Questions 1 and 3 require suffix automata and have similar solutions. Here we only explain questions 2 and 4.
Question 2 is relatively simple: enumerate substrings of \(A\) and feed them into the sequence automaton of \(B\). If not accepted, count it as an answer.
Question 4 requires DP. Let \(f(i, j)\) denote the number of additional characters needed to form a non-common subsequence when currently at state \(i\) in \(A\)'s sequence automaton and state \(j\) in \(B\)'s sequence automaton. The state transition equation is:
The starting point of the transition is \(f(i, \textit{null})=0\).
Reference Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 | |
本页面最近更新:,更新历史
发现错误?想一起完善? 在 GitHub 上编辑此页!
本页面贡献者:OI-wiki
本页面的全部内容在 CC BY-SA 4.0 和 SATA 协议之条款下提供,附加条款亦可能应用