Codeforces1303E. Erase Subsequences
转换一下题意,就相当于问t能不能和s中2个不相同的子串相同,我们可以将t串拆成2个子串t1,t2,得到状态dp[i][j][k]=0/1,表示s判断到i位,t1判断到j位,t2判断到k位,0/1表示是否满足
两个状态,s[i]与t1[j]相匹配,s[i]与t2[k]相匹配
dp[i+1][j+1][k] = dp[i][j][k]
dp[i+1][j][k+1] = dp[i][j][k]
因为dp的值只取0和1,我们可以优化dp函数,dp[i][j][k]可以变成dp[i][j] = k,这样可以消除一维,最后判断dp[s.size()][t1.size()]是否>=t2.size(),也就是匹配成功即可,复杂度为O(n^3)
#include<bits/stdc++.h>
using namespace std;
#define lowbit(x) ((x)&(-x))
typedef long long LL; int dp[][]; bool check(string s, string t) {
int idx = ;
if(!t.size()) return true;
for(int i = ; i < s.size(); ++i) {
if(s[i] == t[idx]) idx++;
if(idx == t.size()) return true;
}
return false;
} bool check(string s, string t1, string t2) {
memset(dp, -, sizeof(dp));
dp[][] = ;
for(int i = ; i < s.size(); ++i)
for(int j = ; j <= t1.size(); ++j)
if(dp[i][j] >= ) {
if(j<t1.size()&&s[i] == t1[j]) dp[i+][j+] = max(dp[i+][j+], dp[i][j]);
if(dp[i][j]<t2.size()&&s[i] == t2[dp[i][j]]) dp[i+][j] = max(dp[i+][j], dp[i][j]+);
dp[i+][j] = max(dp[i][j], dp[i+][j]);
}
int s1 = dp[s.size()][t1.size()], s2 = t2.size();
if(s1 >= s2) return true;
return false;
} void run_case() {
string s, t;
cin >> s >> t;
/* if(check(s, t)) {
cout << "YES\n";
return;
} */
for(int i = ; i < t.size(); ++i) {
string t1 = "", t2 = "";
for(int j = ; j <= i; ++j) {
t1 += t[j];
}
for(int j = i+; j < t.size(); ++j)
t2 += t[j];
if(check(s, t1, t2)) {
cout << "YES\n";
return;
}
}
cout << "NO\n";
} int main() {
ios::sync_with_stdio(false), cin.tie();
//cout.setf(ios_base::showpoint);cout.precision(10);
int t; cin >> t;
while(t--)
run_case();
cout.flush();
return ;
}
Codeforces1303E. Erase Subsequences的更多相关文章
- [CF1303E] Erase Subsequences - dp
Solution 不由分说地枚举分割点 令 \(f[i][j]\) 表示原串处理到 \(i\) ,\(s_1\) 处理到 \(j\),\(s_2\) 最多能处理到哪里 采用主动转移 任意情况, \(f ...
- Codeforces 1303E. Erase Subsequences 代码(dp 字符串压缩一维状态优化)
https://codeforces.com/contest/1303/problem/E #include<bits/stdc++.h> using namespace std; ; i ...
- Educational Codeforces Round 82 (Rated for Div. 2) A-E代码(暂无记录题解)
A. Erasing Zeroes (模拟) #include<bits/stdc++.h> using namespace std; typedef long long ll; ; in ...
- [CF百场计划]#3 Educational Codeforces Round 82 (Rated for Div. 2)
A. Erasing Zeroes Description You are given a string \(s\). Each character is either 0 or 1. You wan ...
- Codeforces题解集 1.0
记录 Codeforces 2019年12月19日到 2020年2月12日 的部分比赛题 Educational Codeforces Round 82 (Rated for Div. 2) D Fi ...
- 2021record
2021-10-14 P2577 [ZJOI2004]午餐 2021-10-13 CF815C Karen and Supermarket(小小紫题,可笑可笑) P6748 『MdOI R3』Fall ...
- 【题解】Educational Codeforces Round 82
比较菜只有 A ~ E A.Erasing Zeroes 题目描述: 原题面 题目分析: 使得所有的 \(1\) 连续也就是所有的 \(1\) 中间的 \(0\) 全部去掉,也就是可以理解为第一个 \ ...
- Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3) D2. Optimal Subsequences (Hard Version) 数据结构 贪心
D2. Optimal Subsequences (Hard Version) This is the harder version of the problem. In this version, ...
- CodeForces - 1183E Subsequences (easy version) (字符串bfs)
The only difference between the easy and the hard versions is constraints. A subsequence is a string ...
随机推荐
- css积累
1. 图片底部3像素的问题解决方案 display: inline-block; vertical-align: bottom;
- Could not set property of class with value There is no setter for property named
检查entity中类的属性与MAPPER中的resultMap属性是否一致
- LeetCode 42接雨水 按行求解(差分+排序)
按行求解的思路比较清晰明了,但是这个方法的复杂度高达O(heightSize*sum(height[i])),几乎高达O(N^2). 但是也并不是不可以解决,经观察我们可以发现,这个算法的缺点在于要遍 ...
- Swagger Learning Notes
背景 首先指定schema[计划的提纲],实时更新最新API,降低集成风险: 早些年:制定word计划文档:前后端分离: 前端测试后端接口:postman 后端提供接口,需要实时更新最新的消息改动 什 ...
- 包、logging模块、hashlib模块、openpyxl模块、深浅拷贝
包.logging模块.hashlib模块.openpyxl模块.深浅拷贝 一.包 1.模块与包 模块的三种来源: 1.内置的 2.第三方的 3.自定义的 模块的四种表现形式: 1.py文件 2.共享 ...
- 【MySQL】数据类型之字符相关
" 目录 字符类型 char类型 varchar类型 实测 总结 枚举类型与集合类型 字符类型 官网:https://dev.mysql.com/doc/refman/5.7/en/char ...
- spring+mybatis报Cannot load JDBC driver
今天做用maven搭建ssm框架的例子,在测试的时候一直报ava.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver 这个异常,找 ...
- Intellij IDEA中创建Package变成一级目录
1.创建包,但是出来的却是一级目录 2.因为Compact Middle Packages默认勾选上了,取消掉即可
- 快速创建vue 项目
随着VUE 技术的不断更新,越来越多的开发者开始使用vue编写前端界面,今天我就和大家分享一下 ,如何快速创建一个vue项目. 前提: 安装了node.js 首先: 全局安装vue-cli 使用命令: ...
- uniGUI学习汇总
UniGUI之UniLabel(31) uniGUI之自定义JS事件动作ClientEvents(30) uniGUI之文件下载(29) uniGUI之FDQuery(28) uniGUI之UniPo ...