ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Codeforces 1150D(字符串dp)

2019-07-02 23:40:39  阅读:175  来源: 互联网

标签:nxt int rep Codeforces Len 1150D include 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;
}

标签:nxt,int,rep,Codeforces,Len,1150D,include,dp
来源: https://www.cnblogs.com/AlphaWA/p/11123797.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有