反思

  • 三维的dp压根没看出来,看题解以后思路又很直观,找几道字符串dp练练才行
  • 序列自动机和优化一维略
  1. /* __ __
  2. * ____| |_____| |____
  3. * | |
  4. * | __ |
  5. * | |
  6. * | > <. |
  7. * | |
  8. * | |
  9. * | ... ⌒ ... |
  10. * | |
  11. * | |
  12. * |___ __|
  13. * | |
  14. * | | Code is far away from bug with the animal protecting
  15. * | | 神兽保佑,代码无bug
  16. * | |
  17. * | |_____
  18. * | |
  19. * | |_
  20. * | _|
  21. * | _|
  22. * |__________________|
  23. * | | | |
  24. */
  25. #pragma comment(linker, "/STACK:1024000000,1024000000")
  26. #include <cstdio>
  27. #include <cstring>
  28. #include <cmath>
  29. #include <cctype>
  30. #include <climits>
  31. #include <iostream>
  32. #include <iomanip>
  33. #include <algorithm>
  34. #include <string>
  35. #include <sstream>
  36. #include <stack>
  37. #include <queue>
  38. #include <set>
  39. #include <map>
  40. #include <vector>
  41. #include <bitset>
  42. #include <random>
  43. #include <functional>
  44. #include <unordered_map>
  45. #define mset(a, b) memset(a, b, sizeof(a))
  46. #define rep(i, a, b) for (int i = a; i <= b; i++)
  47. #define per(i, a, b) for (int i = a; i >= b; i--)
  48. #define fg cout << "--------------\n";
  49. #define debug(x) std::cerr << #x << " = " << x << std::endl
  50. #define All(x) (x.begin()), (x.end())
  51. using namespace std;
  52. typedef double db;
  53. typedef long long ll;
  54. typedef unsigned long long ull;
  55. typedef pair<int, int> pii;
  56. typedef pair<ll, ll> pll;
  57. const int inf = 0x3f3f3f3f;
  58. const ll INF = 1e18;
  59. template <typename T> void read(T &x) {
  60. x = 0;
  61. int s = 1, c = getchar();
  62. for (; !isdigit(c); c = getchar())
  63. if (c == '-') s = -1;
  64. for (; isdigit(c); c = getchar())
  65. x = x * 10 + c - 48;
  66. x *= s;
  67. }
  68. template <typename T> void write(T x) {
  69. if (x < 0) x = -x, putchar('-');
  70. if (x > 9) write(x / 10);
  71. putchar(x % 10 + '0');
  72. }
  73. template <typename T> void writeln(T x) {
  74. write(x);
  75. puts("");
  76. }
  77. const int maxn = 1e5 + 5;
  78. int n, q;
  79. char s[maxn];
  80. int nxt[maxn][30];
  81. const int maxl = 255;
  82. char t[4][maxl];
  83. int Len[4];
  84. int dp[maxl][maxl][maxl];//第一个串匹配到位置i、第二个j、第三个k时,最末端在主串中的位置
  85. int main() {
  86. ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  87. cin >> n >> q >> (s + 1);
  88. rep(i, 0, 25) nxt[n + 1][i] = nxt[n][i] = n + 1;
  89. per(i, n - 1, 0) {
  90. rep(j, 0, 25)
  91. nxt[i][j] = nxt[i + 1][j];
  92. nxt[i][s[i + 1] - 'a'] = i + 1;
  93. }
  94. while (q--) {
  95. char op; int d;
  96. cin >> op >> d;
  97. if (op == '+') {
  98. ++Len[d];
  99. cin >> t[d][Len[d]];
  100. function<void(int&, int)> Min = [&](int &x, int y) {
  101. if (x > y) x = y;
  102. };
  103. rep(i, (d == 1 ? Len[1] : 0), Len[1])
  104. rep(j, (d == 2 ? Len[2] : 0), Len[2])
  105. rep(k, (d == 3 ? Len[3] : 0), Len[3]) {
  106. dp[i][j][k] = n + 1;
  107. if (!i && !j && !k) dp[i][j][k] = 0;
  108. //通过在最短的末端后面加新字符的方式避免了重叠
  109. if (i) Min(dp[i][j][k], nxt[dp[i - 1][j][k]][t[1][i] - 'a']);
  110. if (j) Min(dp[i][j][k], nxt[dp[i][j - 1][k]][t[2][j] - 'a']);
  111. if (k) Min(dp[i][j][k], nxt[dp[i][j][k - 1]][t[3][k] - 'a']);
  112. }
  113. } else {
  114. Len[d]--;
  115. }
  116. cout << (dp[Len[1]][Len[2]][Len[3]] <= n ? "YES\n" : "NO\n");
  117. }
  118. return 0;
  119. }

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. SQLServer索引的四个高级特性

    一Index Building Filter索引创建时过滤 二Index Include Column索引包含列 三聚集索引Cluster Index 四VIEW INDEX视图索引   SQLSer ...

  2. linux-c getopt()参数处理函数

    转自:https://www.cnblogs.com/qingergege/p/5914218.html 最近在弄Linux C编程,本科的时候没好好学啊,希望学弟学妹们引以为鉴. 好了,虽然啰嗦了点 ...

  3. leetcode 376Wiggle Subsequence

    用dp解 1)up定义为nums[i-1] < nums[i] down nums[i-1] > nums[i] 两个dp数组, up[i],记录包含nums[i]且nums[i-1] & ...

  4. 移动端自动化测试Appium 从入门到项目实战Python版

    移动端自动化测试Appium 从入门到项目实战Python版  整个课程都看完了,这个课程的分享可以往下看,下面有链接,之前做java开发也做了一些年头,也分享下自己看这个视频的感受,单论单个知识点课 ...

  5. leetcode 843. Guess the Word

    我做过的第一个 interactive problem 给一个候选词列表,每次猜测可以猜里面的词,会返回猜中匹配的个数, 可以猜10次, 加上随机化策略之后几乎可以一定通过测试(尽管不是100%) c ...

  6. css3之文本和颜色功能之text-shadow

    总本看一下 1.text-shadow 语法:text-shadow: h-shadow v-shadow blur color; h-shadow: 必需.水平阴影的位置.允许负值. v-shado ...

  7. java内部类和静态内部类

    看代码理解内部类,局部内部类和静态内部类 内部类代码: public class InnerTest { private static String name; private int age; pu ...

  8. TZ_05_Spring_基于AOP的xml配置

    1.分析    1>首先我们有一个Service需要增强 将Service增加一个日志(Logger)          2>写了一个日志的通知并且它可以对Service进行日志增强   ...

  9. Mocha测试

    mocha中文名叫做摩卡,是javascript测试的一种常见手段. 其他的类似的测试还有jasmine.karma.tape等. 1. 测试脚本怎么写 // add.js function add( ...

  10. JPinyin繁体相互转换

    // 用正则表达式"[\u4e00-\u9fa5]"匹配 字符串 Scanner sc =new Scanner(System.in);System.out.println(&qu ...