Before reading this article, read Automata first.
Definition
A sequence automaton is an automaton that accepts exactly the subsequences of one string.
Throughout this article, denotes that string.
States
If contains characters, the sequence automaton has states.
Let be a subsequence of . Then is the position where ends at its first occurrence in .
In other words, state represents the set difference between the subsequences of the prefix and those of the prefix .
Every state of a sequence automaton is an accepting state.
Transitions
The state definition gives , the position of the next occurrence of character .
Why the “next” occurrence? If , the subsequences of the suffix form a subset of those of , so choosing the earliest possible occurrence is always optimal.
Implementation
Scan from right to left while maintaining the earliest occurrence of each character:
The construction takes time.
Examples
「HEOI2015」Shortest Non-Common Substring
Given two strings and consisting of lowercase English letters (), find:
- A shortest substring of that is not a substring of ;
- A shortest substring of that is not a subsequence of ;
- A shortest subsequence of that is not a substring of ;
- A shortest subsequence of that is not a subsequence of .
Solution
Questions 1 and 3 require a suffix automaton and use similar methods, so only questions 2 and 4 are discussed here.
Question 2 is straightforward: enumerate substrings of , feed them into the sequence automaton of , and count those that are not accepted.
Question 4 requires DP. Let be the minimum number of additional characters needed to obtain a non-common subsequence when the sequence automaton of is in state and that of is in state . The transition is
The starting condition is .
Reference Code
#include <algorithm>
#include <cstring>
#include <iostream>
#include <string>
using namespace std;
constexpr int N = 2005;
string s, t;
int na[N][26], nb[N][26], nxt[26];
int n, m, a[N], b[N], tot = 1, p = 1, f[N][N << 1];
struct SAM {
int par, ch[26], len;
} sam[N << 1];
void insert(int x) {
int np = ++tot; // new node
sam[np].len = sam[p].len + 1;
while (p && !sam[p].ch[x]) {
sam[p].ch[x] = np;
p = sam[p].par;
}
if (p == 0)
sam[np].par = 1;
else {
int q = sam[p].ch[x];
if (sam[q].len == sam[p].len + 1)
sam[np].par = q;
else {
int nq = ++tot;
sam[nq].len = sam[p].len + 1;
memcpy(sam[nq].ch, sam[q].ch, sizeof(sam[q].ch));
sam[nq].par = sam[q].par;
sam[q].par = sam[np].par = nq;
while (p && sam[p].ch[x] == q) {
sam[p].ch[x] = nq;
p = sam[p].par;
}
}
}
p = np;
}
int main() {
cin >> s >> t;
n = s.size();
m = t.size();
s = " " + s;
t = " " + t;
for (int i = 1; i <= n; ++i) a[i] = s[i] - 'a';
for (int i = 1; i <= m; ++i) b[i] = t[i] - 'a';
for (int i = 1; i <= m; ++i) insert(b[i]);
// nxt[S[i]] <- i
for (int i = 0; i < 26; ++i) nxt[i] = n + 1;
for (int i = n; i >= 0; --i) {
memcpy(na[i], nxt, sizeof(nxt));
nxt[a[i]] = i;
}
for (int i = 0; i < 26; ++i) nxt[i] = m + 1;
for (int i = m; i >= 0; --i) {
memcpy(nb[i], nxt, sizeof(nxt));
nxt[b[i]] = i;
}
// Compute the answers for the four cases.
// 1
int ans = N;
for (int l = 1; l <= n; ++l) {
for (int r = l, u = 1; r <= n; ++r) {
u = sam[u].ch[a[r]];
if (!u) {
ans = min(ans, r - l + 1);
break;
}
}
}
cout << (ans == N ? -1 : ans) << '\n';
// 2
ans = N;
for (int l = 1; l <= n; ++l) {
for (int r = l, u = 0; r <= n; ++r) {
u = nb[u][a[r]];
if (u == m + 1) {
ans = min(ans, r - l + 1);
break;
}
}
}
cout << (ans == N ? -1 : ans) << '\n';
// 3
for (int i = n; i >= 0; --i) {
for (int j = 1; j <= tot; ++j) {
f[i][j] = N;
for (int c = 0; c < 26; ++c) {
int u = na[i][c];
int v = sam[j].ch[c];
if (u <= n) f[i][j] = min(f[i][j], f[u][v] + 1);
}
}
}
cout << (f[0][1] == N ? -1 : f[0][1]) << '\n';
// 4
memset(f, 0, sizeof(f));
for (int i = n; i >= 0; --i) {
for (int j = 0; j <= m; ++j) {
f[i][j] = N;
for (int c = 0; c < 26; ++c) {
int u = na[i][c];
int v = nb[j][c];
if (u <= n) f[i][j] = min(f[i][j], f[u][v] + 1);
}
}
}
cout << (f[0][0] == N ? -1 : f[0][0]) << '\n';
return 0;
}