Codeforces 1150D(字符串dp)

反思

  • 三维的dp压根没看出来,看题解以后思路又很直观,找几道字符串dp练练才行
  • 序列自动机和优化一维略
/*      __      __
 *  ____| |_____| |____
 * |                   |
 * |       __          |
 * |                   |
 * |    >       <.     |
 * |                   |
 * |                   |            
 * |  ...  ⌒   ...     |
 * |                   |
 * |                   |
 * |___             __|
 *   |            |
 *   |            |   Code is far away from bug with the animal protecting
 *   |            |       神兽保佑,代码无bug
 *   |            |
 *   |            |_____
 *   |                  |
 *   |                  |_
 *   |                   _|
 *   |                   _|
 *   |__________________|
 *      | |        | |
 */
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cctype>
#include <climits>
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <string>
#include <sstream>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <vector>
#include <bitset>
#include <random>
#include <functional>
#include <unordered_map>
#define mset(a, b) memset(a, b, sizeof(a))
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define per(i, a, b) for (int i = a; i >= b; i--)
#define fg cout << "--------------\n";
#define debug(x) std::cerr << #x << " = " << x << std::endl
#define All(x) (x.begin()), (x.end())
using namespace std;

typedef double db;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int inf = 0x3f3f3f3f;
const ll INF = 1e18;

template <typename T> void read(T &x) {
    x = 0;
    int s = 1, c = getchar();
    for (; !isdigit(c); c = getchar())
        if (c == '-')   s = -1;
    for (; isdigit(c); c = getchar())
        x = x * 10 + c - 48;
    x *= s;
}

template <typename T> void write(T x) {
    if (x < 0)  x = -x, putchar('-');
    if (x > 9)  write(x / 10);
    putchar(x % 10 + '0');
}

template <typename T> void writeln(T x) {
    write(x);
    puts("");
}

const int maxn = 1e5 + 5;
int n, q;
char s[maxn];
int nxt[maxn][30];

const int maxl = 255;
char t[4][maxl];
int Len[4];
int dp[maxl][maxl][maxl];//第一个串匹配到位置i、第二个j、第三个k时,最末端在主串中的位置

int main() {
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    cin >> n >> q >> (s + 1);

    rep(i, 0, 25)   nxt[n + 1][i] = nxt[n][i] = n + 1;
    per(i, n - 1, 0) {
        rep(j, 0, 25)
            nxt[i][j] = nxt[i + 1][j];
        nxt[i][s[i + 1] - 'a'] = i + 1;
    }
    while (q--) {
        char op; int d;
        cin >> op >> d;
        if (op == '+') {
            ++Len[d];
            cin >> t[d][Len[d]];

            function<void(int&, int)> Min = [&](int &x, int y) {
                if (x > y)  x = y;
            };
            
            rep(i, (d == 1 ? Len[1] : 0), Len[1])
                rep(j, (d == 2 ? Len[2] : 0), Len[2])
                    rep(k, (d == 3 ? Len[3] : 0), Len[3]) {
                        dp[i][j][k] = n + 1;
                        if (!i && !j && !k) dp[i][j][k] = 0;
                        //通过在最短的末端后面加新字符的方式避免了重叠
                        if (i)  Min(dp[i][j][k], nxt[dp[i - 1][j][k]][t[1][i] - 'a']);
                        if (j)  Min(dp[i][j][k], nxt[dp[i][j - 1][k]][t[2][j] - 'a']);
                        if (k)  Min(dp[i][j][k], nxt[dp[i][j][k - 1]][t[3][k] - 'a']);
                    }
        } else {
            Len[d]--;
        }
        cout << (dp[Len[1]][Len[2]][Len[3]] <= n ? "YES\n" : "NO\n");
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/AlphaWA/p/11123797.html