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 ...
随机推荐
- sqlserver查询使用with(nolock)详解
所有Select加 With (NoLock)解决阻塞死锁 在查询语句中使用 NOLOCK 和 READPAST 处理一个数据库死锁的异常时候,其中一个建议就是使用 NOLOCK 或者 READPAS ...
- The Preliminary Contest for ICPC Asia Shanghai 2019 B Light bulbs (离散的差分)
复杂度分析,询问一千次,区间长1e6,O(1e9)超时. 那么我们知道对于差分来说,没必要一个一个求,只需要知道区间长就可以了,所以我们定义结构体差分节点,一个头结点,一个尾节点. 这样tail.lo ...
- ubuntu 安装 gd
最近装一套系统,提示没开启GD, 1.首先检查一下,是否安装 新建一个文件 <?php phpinfo(); ?> 如果安装了,会在页面显示 2.没安装当然没有了 这个安装也是根据php版 ...
- celery参考
1,https://www.wandouip.com/t5i377365/ 2,https://www.cnblogs.com/zhangmingcheng/p/6050270.html (syste ...
- python小白的爬虫之旅
1.爬www.haha56.net/main/youmo网站的内容 ieimport requests import re response=requests.get("http://www ...
- C/C++ 传递信息给Java
#开始 今天有人问我C++怎么给Java传递消息 大概需求是 用C++写了一个窗口 需要把编辑框里面的东西传递给Java做处理 #解决过程 我现在能想到的有三个简单的方法 1. Socket编程 通过 ...
- 实现在vue中element-ui的el-dialog弹框拖拽
参考:实现在vue中element-ui的el-dialog弹框拖拽 1.在 utils 中新建 directives.js 文件 import Vue from 'vue' // v-dialogD ...
- 【原】python-jenkins信息
1.官方文档: https://python-jenkins.readthedocs.io/en/latest/examples.html#example-1-get-version-of-jenki ...
- 猜解数据库(MYSQL)信息
/Less-1/?id=1' and if (length(database())=8,sleep(5),0) --+ 注:http://43.247.91.228:84/Less-1/为靶场地址,发 ...
- 05-Docker-Container资源限制
目录 05-Docker-Container资源限制 参考 可压缩性 MEM限制 选项说明 压测示例 CPU限制 选项说明 压测示例 05-Docker-Container资源限制 Docker Ve ...