序列自动机

序列自动机

判断字符串\(s\)是否存在序列\(t\)。只需要记录这个位置,后面每个字符出现的最早位置即可\(nxt\)

  • 求子序列个数
    记忆化搜索\(f[x] = \underset {y \in x'son}{\sum} f[y] + 1\)
  • 求两串的公共子序列个数
    两个串的记忆化搜搜\(f[x][y]\)

例题:
luogu: P5826 【模板】子序列自动机

由于值域原因这题需要用主席树实现。

void insert(int l, int r, int pre, int& cur, const int& pos, const int& to) {
    nxt[++tot] = nxt[pre];
    L[tot] = L[pre];
    R[tot] = R[pre];
    cur = tot;
    if (l == r) { nxt[tot] = to; return; }
    int m = (l + r) >> 1;
    if (pos > m)insert(m + 1, r, R[pre], R[cur], pos, to);
    else insert(l, m, L[pre], L[cur], pos, to);
}
int query(int l, int r, int cur, const int& pos) {
    if (l == r)return nxt[cur];
    int m = (l + r) >> 1;
    if (m >= pos)return query(l, m, L[cur], pos);
    else return query(m + 1, r, R[cur], pos);
}
posted on 2024-07-22 22:16  Quixotica  阅读(2)  评论(0编辑  收藏  举报