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;
}
Codeforces 1150D(字符串dp)的更多相关文章
- Maximum Questions CodeForces - 900E (字符串,dp)
大意:给定长$n$的字符串$s$, 只含'a','b','?', '?'可以替换为任意字符, 在给定长$t$的字符串, "ababab...", 求替换尽量少的'?', 使得$s$ ...
- Three Religions CodeForces - 1149B (字符串,dp)
大意: 给定字符串S, 要求维护三个串, 支持在每个串末尾添加或删除字符, 询问S是否能找到三个不相交的子序列等于三个串. 暴力DP, 若不考虑动态维护的话, 可以直接$O(len^3)$处理出最少需 ...
- Dreamoon and Strings CodeForces - 477C (字符串dp)
大意: 给定字符串$s$, $p$, 对于$0\le x\le |s|$, 求$s$删除$x$个字符后, $p$在$s$中的最大出现次数. 显然答案是先递增后递减的, 那么问题就转化求最大出现次数为$ ...
- Erasing Substrings CodeForces - 938F (字符串dp)
大意: 给定字符串$s$, 长度为$n$, 取$k=\lfloor log2(n)\rfloor$, 第$i$次操作删除一个长度为$2^{i-1}$的子串, 求一种方案使得, $k$次操作后$s$的字 ...
- Codeforces 176B (线性DP+字符串)
题目链接: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=28214 题目大意:源串有如下变形:每次将串切为两半,位置颠倒形成 ...
- codeforces#1150D. Three Religions(dp+序列自动机)
题目链接: https://codeforces.com/contest/1150/problem/D 题意: 给出长度为$n$的字符串,和$q$次询问 每次询问是,给$x$宗教增加一个字符$key$ ...
- 【BZOJ 2121】 (字符串DP,区间DP)
2121: 字符串游戏 Description BX正在进行一个字符串游戏,他手上有一个字符串L,以及其他一些字符串的集合S,然后他可以进行以下操作:对于一个在集合S中的字符串p,如果p在L中出现,B ...
- AtCoder Regular Contest 081 E - Don't Be a Subsequence(字符串DP)
引用自:onion_cyc 字符串DP一直不是强项...以后没思路的题就想DP和网络流23333333 f[i]表示从i开始的后缀非子序列的最短长度 pos[i][j]表示从i开始的j字符最早出现位 ...
- NOIP2015Day2T2子串(字符串dp)
又被“if(a=b)”坑了QAQ...写C++还是得开Warning,这么久了pascal还没改过来咋回事啊QWQ 题目大意就不说了OWO 网上的题解都不怎么看得懂啊...好像写得都很乱?还是我太sb ...
随机推荐
- SQLServer索引的四个高级特性
一Index Building Filter索引创建时过滤 二Index Include Column索引包含列 三聚集索引Cluster Index 四VIEW INDEX视图索引 SQLSer ...
- linux-c getopt()参数处理函数
转自:https://www.cnblogs.com/qingergege/p/5914218.html 最近在弄Linux C编程,本科的时候没好好学啊,希望学弟学妹们引以为鉴. 好了,虽然啰嗦了点 ...
- 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] & ...
- 移动端自动化测试Appium 从入门到项目实战Python版
移动端自动化测试Appium 从入门到项目实战Python版 整个课程都看完了,这个课程的分享可以往下看,下面有链接,之前做java开发也做了一些年头,也分享下自己看这个视频的感受,单论单个知识点课 ...
- leetcode 843. Guess the Word
我做过的第一个 interactive problem 给一个候选词列表,每次猜测可以猜里面的词,会返回猜中匹配的个数, 可以猜10次, 加上随机化策略之后几乎可以一定通过测试(尽管不是100%) c ...
- css3之文本和颜色功能之text-shadow
总本看一下 1.text-shadow 语法:text-shadow: h-shadow v-shadow blur color; h-shadow: 必需.水平阴影的位置.允许负值. v-shado ...
- java内部类和静态内部类
看代码理解内部类,局部内部类和静态内部类 内部类代码: public class InnerTest { private static String name; private int age; pu ...
- TZ_05_Spring_基于AOP的xml配置
1.分析 1>首先我们有一个Service需要增强 将Service增加一个日志(Logger) 2>写了一个日志的通知并且它可以对Service进行日志增强 ...
- Mocha测试
mocha中文名叫做摩卡,是javascript测试的一种常见手段. 其他的类似的测试还有jasmine.karma.tape等. 1. 测试脚本怎么写 // add.js function add( ...
- JPinyin繁体相互转换
// 用正则表达式"[\u4e00-\u9fa5]"匹配 字符串 Scanner sc =new Scanner(System.in);System.out.println(&qu ...