题解:P11611 [PA 2016] 归约 / CNF-SAT

· · 题解

提供一种不同于第一篇题解的另一种做法。

做法使用了 >.< 这道题目的解法。

题目大意

经过转化之后可以得到:

对于一个长度为 n 的 01 串 T,存在如下 m 条限制:

  • 给定一个区间 [l, r] 以及一个长度为 r - l + 1 的 01 串 s,需要存在至少一个位置 l \le i \le r,使得 T_i \ne s_{i - l + 1}。

求出满足所有 m 条限制的字符串 T 的个数。

思路分析

难点在于第一步的转换。我们对于每一个位置建立两个点 (i - 1) \times 2 + 1 和 (i - 1) \times 2 + 2,分别表示这个位置填入 0 或者填入 1。

然后相邻位置进行连边 ((i - 1) \times 2 + (1/2),\ i \times 2 + (1/2))。

那么从 1/2 到达 (n - 1) \times 2 + (1/2) 的一条简单路径,就对应了一个 01 串 T。

我们把一条路径的点的编号按顺序写下来构成一个字符串,那么限制就要求所有子串不能出现一个给定的字符串。

例如,一个 T = 010,那么路径的字符串就是 1, 4, 5。

例如:[2, 3],s = 01,就要求这个字符串不能存在子串 3, 6。

那么因为有限制:不能出现子串,所以可以想到 AC 自动机。

我们对于所有的限制建立 AC 自动机,在自动机上的走路就会走出一个字符串 T。

那么问题变成在 AC 自动机上路径计数,要求不能经过插入的字符串的末尾的点(因为这些点一旦经过就表明匹配到了一个字符串)。

这个 AC 自动机是个 DAG,所以可以直接拓扑求解。

这是大体的思路。

实现细节上,我们发现这个东西是会分成 n 层的,所以只用记录两个儿子就可以了,再对于构建时 fail = 0 的转移进行一些特判就可以了,具体实现可以看我的代码,也就是说实际上的字符集没有这么多,边数也没有很多。

所以点数是 O(n + L),边数也是 O(n + L) 级别的。

所以时空复杂度是 O(n + L)。

代码呈现


#include <iostream>
#include <cstring>
#include <cstdio>
#include <map>
#define get(x, c) (fail[x] ? ch[fail[x]][c] : ed[x] * 2 + 1 + c)

const int N = 1e6 + 10;
const int M = 3e6 + 10;
const int mod = 1e9 + 7;
int n, cnt, ans, s[N];
int ch[M][2], fail[M], ed[M];
int q[M], top, f[M], in[M];
bool vis[M];

inline void Add(int &x, int y) {
    x += y;
    if (x >= mod) x -= mod;
}

void ins(int l, int r) {
    int pos = 2 * (l - 1) + s[l] + 1;
    for (int i = l + 1; i <= r; i++) {
        if (!ch[pos][s[i]]) 
            ch[pos][s[i]] = ++cnt, ed[cnt] = i;
        pos = ch[pos][s[i]];
    }
    vis[pos] = true;
}

void getfail() {
    for (int i = 1; i <= n * 2; i++) 
        q[++top] = i;

    for (int id = 1; id <= top; id++) {
        int x = q[id];
        vis[x] |= vis[fail[x]];
        if (ed[x] == n) continue;
        if (ch[x][0]) {
            fail[ch[x][0]] = get(x, 0);
            q[++top] = ch[x][0];
        } else ch[x][0] = get(x, 0);
        if (ch[x][1]) {
            fail[ch[x][1]] = get(x, 1);
            q[++top] = ch[x][1];
        } else ch[x][1] = get(x, 1);
        in[ch[x][1]]++, in[ch[x][0]]++;
    }
}

int main() {
    std::cin >> n;
    cnt = n * 2;
    for (int i = 1; i <= n * 2; i++) ed[i] = (i - 1) / 2 + 1;

    int now = 0, opt = 0, mn, mx;
    bool flag = false; char CH;
    while ((CH = getchar()) != EOF) {
        if (opt && (CH < '0' || CH > '9')) {
            s[now] = flag; 
            mn = std::min(mn, now), mx = std::max(mx, now);
            flag = false; opt = now = 0;
        }
        if (CH == ' ' || CH == '\n') continue;
        if (CH >= '0' && CH <= '9') now = now * 10 + CH - '0';
        else if (CH == ')') {
            ins(mn, mx);
        } else if (CH == 'x') opt = 1;
        else if (CH == '~') flag = true;
        else if (CH == '(') mn = n + 1, mx = 0;
    }
    getfail();
    top = 0;
    f[1] = f[2] = 1;
    for (int i = 1; i <= cnt; i++)
        if (!in[i]) q[++top] = i;

    for (int id = 1; id <= top; id++) {
        int x = q[id];
        if (vis[x]) f[x] = 0;
        if (ed[x] == n) {
            Add(ans, f[x]);
            continue;
        }
        for (int i = 0; i < 2; i++) {
            int to = ch[x][i];
            Add(f[to], f[x]);
            in[to]--;
            if (!in[to]) q[++top] = to;
        }
    }
    std::cout << ans;
    return 0;
}