转换一下题意,就相当于问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的更多相关文章

  1. [CF1303E] Erase Subsequences - dp

    Solution 不由分说地枚举分割点 令 \(f[i][j]\) 表示原串处理到 \(i\) ,\(s_1\) 处理到 \(j\),\(s_2\) 最多能处理到哪里 采用主动转移 任意情况, \(f ...

  2. Codeforces 1303E. Erase Subsequences 代码(dp 字符串压缩一维状态优化)

    https://codeforces.com/contest/1303/problem/E #include<bits/stdc++.h> using namespace std; ; i ...

  3. 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 ...

  4. [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 ...

  5. Codeforces题解集 1.0

    记录 Codeforces 2019年12月19日到 2020年2月12日 的部分比赛题 Educational Codeforces Round 82 (Rated for Div. 2) D Fi ...

  6. 2021record

    2021-10-14 P2577 [ZJOI2004]午餐 2021-10-13 CF815C Karen and Supermarket(小小紫题,可笑可笑) P6748 『MdOI R3』Fall ...

  7. 【题解】Educational Codeforces Round 82

    比较菜只有 A ~ E A.Erasing Zeroes 题目描述: 原题面 题目分析: 使得所有的 \(1\) 连续也就是所有的 \(1\) 中间的 \(0\) 全部去掉,也就是可以理解为第一个 \ ...

  8. 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, ...

  9. CodeForces - 1183E Subsequences (easy version) (字符串bfs)

    The only difference between the easy and the hard versions is constraints. A subsequence is a string ...

随机推荐

  1. ExecutorService 的Future类

    1.概述 在本文中,我们将了解Future.自Java 1.5以来一直存在的接口,在处理异步调用和并发处理时非常有用. 2.创建Future 简单地说,Future类表示异步计算的未来结果 - 这个结 ...

  2. JS知识点查漏补缺

    知识点1: 判断语句中遇到NaN即为 False 只需要注意遇到False即为False即可 使用join(),toString()皆可以将数组转化为字符串 二者的相同点在于都可以转化数组为字符串 二 ...

  3. js分享微信 ,微博 ,qq空间

    目前pc微信分享,是通过扫描二维码进行分享 var _title,_source,_sourceUrl,_pic,_showcount,_desc,_summary,_site, _width = , ...

  4. VBA 学习笔记 - 判断语句、循环

    判断语句 大部分和 Lua 差不多,多了一个 Switch 语句 循环 For 循环 多次执行一系列语句,缩写管理循环变量的代码. For i = start To end [Step X]...Ne ...

  5. c#本地缓存当数据库表更改时,缓存失效。

    web.config <?xml version="1.0" encoding="utf-8"?> <!-- 有关如何配置 ASP.NET 应 ...

  6. 浏览器 User-Agent 整理

    也可以去这里查询:http://tools.jb51.net/table/useragent window.navigator.userAgent 1) Chrome Win7: Mozilla/5. ...

  7. vultr安装kali

    前言 很多国内的主机不支持自定义安装系统,且也不方便下载国外资料:),所以需要使用vultr安装kali. 1.上传镜像 镜像地址填这个(我当时的最新版本) https://cdimage.kali. ...

  8. 吴裕雄 PYTHON 神经网络——TENSORFLOW 滑动平均模型

    import tensorflow as tf v1 = tf.Variable(0, dtype=tf.float32) step = tf.Variable(0, trainable=False) ...

  9. POJ1797 Heavy Transportation (堆优化的Dijkstra变形)

    Background Hugo Heavy is happy. After the breakdown of the Cargolifter project he can now expand bus ...

  10. Laravel Vuejs 实战:开发知乎 (1)项目环境配置和用户表设计

    1.使用laragon新建laravel项目 zhihu 2.配置env文件的database设置 DB_DATABASE=zhihu 3.分析users表需要的字段 4.修改数据库迁移文件: cla ...