P4590: [TJOI2018] Garden Party — Solution
DP of DP
I kept having this feeling that I'd done it before in Group A at Jizhong, and it turned out I really had; I just didn't know back then that this thing was called DP of DP.
If we ignore matching up to position \(i\) and only consider preventing the substring \(NOI\) from appearing, we can directly match and count with \(dp[i][j]\). That part is easy. The main thing is how to describe the state of the subsequence matching. Similar string problems can be done on an automaton, but we don't have such an automaton. We consider using DP of DP.
So how do we do DP of DP? Let's think about the process of matching the longest subsequence:
\[ dp[i][j] = \max \begin{cases} dp[i - 1][j] \\ dp[i][j - 1] \\ dp[i - 1][j - 1] + 1, (S_i = T_j) \end{cases} \]
It would be great if we could cram this thing in. First, the \(i\) of the two DPs can be shared, and what we need to describe is limited to this \(i\) too, which is very nice. Let \(dp[i][j][STAT]\) mean that the first \(i\) positions have been computed, the match with \(NOI\) has reached position \(j\), and the match with the string is \(STAT\). This \(STAT\) records which positions in string \(T\) have been matched. (Those matched positions can be worked out with an extra DP.)
Code
// P4590.cpp
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 1010, MAX_M = 15, mod = 1e9 + 7;
const int nxt_len[3][3] = {{1, 0, 0}, {1, 2, 0}, {1, 0, 3}};
int n, m, dp[2][1 << MAX_M][3], trans[1 << MAX_M][3], f[2][MAX_N], org[MAX_N], ans[MAX_N];
char str[MAX_N];
void build()
{
for (int stat = 0; stat < (1 << m); stat++)
{
for (int i = 0; i < m; i++)
f[0][i + 1] = f[0][i] + ((stat >> i) & 1);
for (int i = 0; i < 3; i++)
{
// get the new stat after choosing char i;
for (int j = 1; j <= m; j++)
{
f[1][j] = max(f[1][j - 1], f[0][j]);
if (i == org[j])
f[1][j] = max(f[1][j], f[0][j - 1] + 1);
}
int nxt = 0;
for (int j = 1; j <= m; j++)
nxt |= (f[1][j] - f[1][j - 1]) << (j - 1);
trans[stat][i] = nxt;
}
}
}
int main()
{
scanf("%d%d%s", &n, &m, str + 1);
for (int i = 1; i <= m; i++)
org[i] = (str[i] == 'N' ? 0 : (str[i] == 'O' ? 1 : 2));
build(), dp[0][0][0] = 1;
for (int i = 1; i <= n; i++)
{
memset(dp[i & 1], 0, sizeof(dp[i & 1]));
for (int j = 0; j < (1 << m); j++)
for (int len = 0; len < 3; len++)
for (int c = 0; c < 3; c++)
if (!(len == 2 && c == 2))
dp[i & 1][trans[j][c]][nxt_len[len][c]] = (0LL + dp[i & 1][trans[j][c]][nxt_len[len][c]] + dp[!(i & 1)][j][len]) % mod;
}
for (int stat = 0; stat < (1 << m); stat++)
for (int len = 0; len < 3; len++)
ans[__builtin_popcount(stat)] = (0LL + ans[__builtin_popcount(stat)] + dp[n & 1][stat][len]) % mod;
for (int i = 0; i <= m; i++)
printf("%d\n", ans[i]);
return 0;
}