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 ...
随机推荐
- PHPstorm同步服务器代码的缺点---命名空间undefined
在把一个服务器的代码同步到phpstorm下开发的时候,发现新建的命名空间代码都失效了,然而换到 https://blog.csdn.net/afraid_to_have/article/deta ...
- memcache课程---4、php+memcache如何让用户跨域登录
memcache课程---4.php+memcache如何让用户跨域登录 一.总结 一句话总结: 让所有服务器共用一台memcache缓存,即可达到跨域的目的 1.session跨域:修改php配置文 ...
- Android开发随笔
1.线性布局LinearLayout时,用到layout_weight权重的使用 控件的宽度(高度)=自身宽度(高度)+剩余空间的所占比例 剩余空间(可以为负值)=屏幕宽-所有控件宽度(高度)< ...
- 引用不了XXservice,怎么办?
1.tEdasArchiveLogService = (TEdasArchiveLogService) SpringContextHolder.getBean("TEdasArchiveLo ...
- np一些基本操作1
##生成一个一维数组import numpy as np;nb7 = np.arange(0,100,2);print(nb7)print("======================== ...
- Liunx常用命令行(Ubuntu)
关闭防火墙的命令行: 1. 永久性生效 开启:chkconfig iptables on 关闭:chkconfig iptables off 2. 即时生效,重启后失效 开启:service ipta ...
- 2019-6-27-WPF-如何给定两个点画出一条波浪线
title author date CreateTime categories WPF 如何给定两个点画出一条波浪线 lindexi 2019-6-27 10:17:6 +0800 2019-6-26 ...
- 安卓自定义View进阶-Canvas之画布操作 转载
安卓自定义View进阶-Canvas之画布操作 转载 https://www.gcssloop.com/customview/Canvas_Convert 本来想把画布操作放到后面部分的,但是发现很多 ...
- day66作业
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- ROCR包中ROC曲线计算是取大于cutoff还是大于等于cutoff
找到对应的代码如下 .compute.unnormalized.roc.curve function (predictions, labels) { pos.label <- levels(la ...