反思

  • 三维的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;
}

Codeforces 1150D(字符串dp)的更多相关文章

  1. Maximum Questions CodeForces - 900E (字符串,dp)

    大意:给定长$n$的字符串$s$, 只含'a','b','?', '?'可以替换为任意字符, 在给定长$t$的字符串, "ababab...", 求替换尽量少的'?', 使得$s$ ...

  2. Three Religions CodeForces - 1149B (字符串,dp)

    大意: 给定字符串S, 要求维护三个串, 支持在每个串末尾添加或删除字符, 询问S是否能找到三个不相交的子序列等于三个串. 暴力DP, 若不考虑动态维护的话, 可以直接$O(len^3)$处理出最少需 ...

  3. Dreamoon and Strings CodeForces - 477C (字符串dp)

    大意: 给定字符串$s$, $p$, 对于$0\le x\le |s|$, 求$s$删除$x$个字符后, $p$在$s$中的最大出现次数. 显然答案是先递增后递减的, 那么问题就转化求最大出现次数为$ ...

  4. Erasing Substrings CodeForces - 938F (字符串dp)

    大意: 给定字符串$s$, 长度为$n$, 取$k=\lfloor log2(n)\rfloor$, 第$i$次操作删除一个长度为$2^{i-1}$的子串, 求一种方案使得, $k$次操作后$s$的字 ...

  5. Codeforces 176B (线性DP+字符串)

    题目链接: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=28214 题目大意:源串有如下变形:每次将串切为两半,位置颠倒形成 ...

  6. codeforces#1150D. Three Religions(dp+序列自动机)

    题目链接: https://codeforces.com/contest/1150/problem/D 题意: 给出长度为$n$的字符串,和$q$次询问 每次询问是,给$x$宗教增加一个字符$key$ ...

  7. 【BZOJ 2121】 (字符串DP,区间DP)

    2121: 字符串游戏 Description BX正在进行一个字符串游戏,他手上有一个字符串L,以及其他一些字符串的集合S,然后他可以进行以下操作:对于一个在集合S中的字符串p,如果p在L中出现,B ...

  8. AtCoder Regular Contest 081 E - Don't Be a Subsequence(字符串DP)

    引用自:onion_cyc 字符串DP一直不是强项...以后没思路的题就想DP和网络流23333333 f[i]表示从i开始的后缀非子序列的最短长度  pos[i][j]表示从i开始的j字符最早出现位 ...

  9. NOIP2015Day2T2子串(字符串dp)

    又被“if(a=b)”坑了QAQ...写C++还是得开Warning,这么久了pascal还没改过来咋回事啊QWQ 题目大意就不说了OWO 网上的题解都不怎么看得懂啊...好像写得都很乱?还是我太sb ...

随机推荐

  1. 信息安全-浏览器-CORS:CORS(跨域资源共享)

    ylbtech-信息安全-浏览器-CORS:CORS(跨域资源共享) 1.返回顶部 1. CORS,全称Cross-Origin Resource Sharing,是一种允许当前域(domain)的资 ...

  2. 《DSP using MATLAB》Problem 8.17

    代码: %% ------------------------------------------------------------------------ %% Output Info about ...

  3. 封装了一个HOOKAPI的类。。。

    我惊奇地发现,我手竟然这么生了.....   很久很久没有写这方面的东西,现在写着竟然这么手生....哎....   这玩艺,还得天天练....

  4. windows 常用的快捷键

    记录一些 windows 常用快捷键,待更新 Ctrl系列 快捷键 功能 Ctrl + C 复制 Ctrl + INSERT 复制 Ctrl + V 粘贴 Ctrl + Z 撤销 Ctrl + D 删 ...

  5. cannot be cast to javax.servlet.Servlet 解决

    使用maven创建web项目的时候,通过添加依赖的方式来添加servlet-api,如下 通过maven的命令(tomcat:run)来启动项目,发现访问的时候报错,错误如下: 错误排查: 首先查看s ...

  6. 廖雪峰Java9正则表达式-2正则表达式进阶-6搜索和替换

    1.使用正则表达式分割字符串: String[] string.split(String regex); "a b c".split("\\s");->[ ...

  7. csp-s模拟测试b组加餐antipalindome,randomwalking,string题解

    题面:https://www.cnblogs.com/Juve/articles/11599318.html antipalindome: 打表找规律? 对于一个回文串,我们只要保证3位以内不回文即可 ...

  8. Bash新技能

    1. 输出数组全部元素 echo ${array_name[@]} 2. 输出数组长度 echo ${#array_name[@]} #获得数组长度 echo ${#string_name} #获得字 ...

  9. 页面自动执行(加载)js的几种方法

    https://www.cnblogs.com/2huos/p/js-autorun.html 一.JS方法1.最简单的调用方式,直接写到html的body标签里面: <html> < ...

  10. 【JSOI2018】绝地反击

    题面 50pts 首先当然是二分答案\(mid\), 对于每一个点,以它为圆心的圆,交上攻击轨道: 那么这个点到攻击轨迹的可达范围就是一段圆弧. 怎么求这段圆弧呢? 我们知道圆弧可以用其两端点对于圆心 ...