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 ...
随机推荐
- tp5 自定义公共函数,前台模板调用
最近用tp5做一个cms,在添加模型的时候,选择类型,这类型太多了,如果一个个的去判断显示,能累死人了,干脆写个公共方法, 首先写公共方法用到Common.php,目录project/applicat ...
- java篇 之 多态
2018-9-28 多态: 重载也称为静态多态(静态在编译阶段就能确定)(动态是跟运行时挂钩) 尽量去选择关系轻的,降低耦合度(紧密度) 内聚: 减少与外界的联系,降低与其他对象和类的联系 对象与对象 ...
- P1426
和上次的小鱼题差不多,但多了一些条件. 先把游到 $ s - x $ 米是第 $ a_i $ 秒求出来,然后判断之后在第 $ a_{i + 1} $ 秒内游的距离是否 $ \geq 2x $ ,大于就 ...
- java.lang.ClassNotFoundException: javax.xml.bind.DatatypeConverter 可能是我们运行的java版本过高导致
最近给自己写了个 SpringBoot 应用程序 顺便练练手,准备把程序过到 树莓派 上,结果登陆报错
- 简单oracle查询语句转换为mongo查询语句
可以将简单的单表查询语句转换成Mongo过滤条件 列: 1. db.demo.aggregate([ {"$match": {"$and": [{"p ...
- rsync安装与配置使用 数据同步方案(centos6.5)
rsync + crond ==定时数据同步 sersync(inotify) + rsync ==实时数据同步,利用rsync实现 ##应用场景 ..1 主备服务器之间同步数据定时 = ...
- DL4J之CNN对今日头条文本分类
一.数据集介绍 数据来源:今日头条客户端 数据格式如下: 6551700932705387022_!_101_!_news_culture_!_京城最值得你来场文化之旅的博物馆_!_保利集团,马未都, ...
- December 28th, Week 52nd Saturday, 2019
If you start at the bottom, pay your dues, life here can be a dream come true. 只要你从头开始,脚踏实地,梦想是可以成真的 ...
- Java后台技术IBATIS入门
做过.net后台开发的同志一定用过Entity FrameWork,该框架实现了实体Entity到数据库行的映射,通过操作实体DataSet,就能够直接同步修改到数据库.但是Java暂时没有类似的技术 ...
- Mongodb 分片原理
1.主从mongodb 模式 类似,MySQL的主从配置 参照:https://blog.csdn.net/liusong0605/article/details/11551699 mongoDB有 ...