Olympiad in Informatics

[BZOJ5451 / QuestOJ 2152] String — Solution

Main Idea

This problem is pretty interesting, and it has some weird details.

First, considering only the forward strings, we just stuff them straight into an Aho–Corasick automaton and do the standard-trick DP. Now consider a string lying in the second half: it turns out that as long as you characterize the first half properly, the second half gets characterized automatically, so we take each string's reverse, complement its characters, and stuff it into the forward strings' Aho–Corasick automaton, and that lets the string appear in the second half.

The tricky part is the middle position. First, notice that a string can only be placed across the middle under certain conditions; these positions can be worked out (obviously, the left part cut off by the dividing line obviously has to be longer than the right part, because we want the whole configuration to be characterized stably) and stuffed into the automaton. However, here we compress them into a bitmask in a separate array, because this bitmask information is only useful at the \(m\)-th position.

As for why we create two new nodes at the very start, the reason is this: consider 001 from the sample, with the cut between positions 2 and 3. Because of the length issue, we need to flip it into 011, with the cut between positions 1 and 2. If we don't create two new nodes in reserve, then 01 cannot be represented (if we don't create them deliberately, this automaton has no node of length 1 whose content is 1).

Code

// C.cpp
#include <bits/stdc++.h>

using namespace std;

const int MAX_N = 7, MAX_LEN = 110, MAX_M = 550, MAX_NODE = MAX_LEN * MAX_N << 1, mod = 998244353;

int n, m, ptot, ch[MAX_NODE][2], tag_pos[MAX_NODE], tag_end[MAX_NODE], fail[MAX_NODE], dp[MAX_M][MAX_NODE][1 << MAX_N];
char org[MAX_N][MAX_LEN];

void insert(char *str, int idx)
{
    int p = 0;
    for (int i = 1; str[i]; i++)
    {
        if (ch[p][str[i] - '0'] == 0)
            ch[p][str[i] - '0'] = ++ptot;
        p = ch[p][str[i] - '0'];
    }
    tag_pos[p] |= (1 << idx);
}

void build_fail()
{
    queue<int> q;
    for (int i = 0; i <= 1; i++)
        if (ch[0][i])
            q.push(ch[0][i]);
    while (!q.empty())
    {
        int u = q.front();
        q.pop();
        for (int i = 0; i <= 1; i++)
            if (ch[u][i])
            {
                fail[ch[u][i]] = ch[fail[u]][i];
                tag_pos[ch[u][i]] |= tag_pos[fail[ch[u][i]]], tag_end[ch[u][i]] |= tag_end[fail[ch[u][i]]];
                q.push(ch[u][i]);
            }
            else
                ch[u][i] = ch[fail[u]][i];
    }
}

int main()
{
    scanf("%d%d", &n, &m), ch[0][0] = ++ptot, ch[0][1] = ++ptot;
    for (int i = 1; i <= n; i++)
        scanf("%s", org[i] + 1), insert(org[i], i - 1);
    for (int i = 1; i <= n; i++)
    {
        // insert the splitting shit;
        int len = strlen(org[i] + 1), p = 0;
        for (int sptr = 1; sptr < ((len + 1) >> 1); sptr++)
            p = ch[p][org[i][sptr] - '0'];
        for (int sptr = (len + 1) >> 1; sptr < len; sptr++)
        {
            // [1, sptr] || [sptr + 1, len];
            p = ch[p][org[i][sptr] - '0'];
            int lptr = sptr, rptr = sptr + 1;
            bool flag = true;
            while (lptr >= 1 && rptr <= len)
                flag &= (org[i][lptr] != org[i][rptr]), lptr--, rptr++;
            if (flag)
                tag_end[p] |= (1 << (i - 1));
        }
    }
    for (int i = 1; i <= n; i++)
    {
        int len = strlen(org[i] + 1);
        reverse(org[i] + 1, org[i] + 1 + len);
        for (int j = 1; j <= len; j++)
            org[i][j] ^= 1;
        insert(org[i], i - 1);
        int p = 0;
        for (int sptr = 1; sptr < (len >> 1) + 1; sptr++)
            p = ch[p][org[i][sptr] - '0'];
        for (int sptr = (len >> 1) + 1; sptr < len; sptr++)
        {
            // [1, sptr] || [sptr + 1, len];
            p = ch[p][org[i][sptr] - '0'];
            int lptr = sptr, rptr = sptr + 1;
            bool flag = true;
            while (lptr >= 1 && rptr <= len)
                flag &= (org[i][lptr] != org[i][rptr]), lptr--, rptr++;
            if (flag)
                tag_end[p] |= (1 << (i - 1));
        }
    }
    build_fail();
    dp[0][0][0] = 1;
    for (int i = 0; i < m; i++)
        for (int p = 0; p <= ptot; p++)
            for (int c = 0; c < 2; c++)
                if (ch[p][c])
                    for (int mask = 0; mask < (1 << n); mask++)
                        dp[i + 1][ch[p][c]][mask | tag_pos[ch[p][c]]] = (0LL + dp[i + 1][ch[p][c]][mask | tag_pos[ch[p][c]]] + dp[i][p][mask]) % mod;
    int ans = 0;
    for (int p = 0; p <= ptot; p++)
        for (int mask = 0; mask < (1 << n); mask++)
            if (dp[m][p][mask])
                if ((mask | tag_end[p]) == (1 << n) - 1)
                    ans = (0LL + ans + dp[m][p][mask]) % mod;
    printf("%d\n", ans);
    return 0;
}