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 ...
随机推荐
- 安慰奶牛Cheering up the Cow
传送门 一次a就很开心 可以当作kruskal模板题(orz --------------------------------------------------------------------- ...
- 安卓开发:图片的显示Mode
安卓开发中将图片放置在ImageView中展示,涉及到图片显示的模式,跟iOS开发中UIView的contentMode属性是一个意思,在安卓UI元素中使用的属性是scaleType,其中枚举值的效果 ...
- wordpress 支持上传中文名称文件
添加文章难免要传个图.文件啥的,可是呢,上传中文名称的文件竟然不行,找了半天,中文乱码,脑残了,竟然忘了这个事,哎 修改其实很简单,只需要两步 1./wp-admin/includes/file.ph ...
- 【PAT甲级】1098 Insertion or Heap Sort (25 分)
题意: 输入一个正整数N(<=100),接着输入两行N个数,表示原数组和经过一定次数排序后的数组.判断是经过插入排序还是堆排序并输出再次经过该排序后的数组(数据保证答案唯一). AAAAAcce ...
- 「HNOI2012」永无乡
传送门 Luogu 解题思路 很容易想到平衡树,然后还可以顺便维护一下连通性,但是如何合并两棵平衡树? 我们采用一种类似于启发式合并的思想,将根节点siz较小的那颗平衡树暴力的合并到另一颗上去. 那么 ...
- Spark性能调优-高级篇
前言 继基础篇讲解了每个Spark开发人员都必须熟知的开发调优与资源调优之后,本文作为<Spark性能优化指南>的高级篇,将深入分析数据倾斜调优与shuffle调优,以解决更加棘手的性能问 ...
- spring data flow
spring data flow相当于一个快速发布应用的平台.并可以通过消息队列(kafa,rabbitMQ)把多个应用链接在一起进行链式处理数据.支持的平台是: Cloud Foundry Apac ...
- win 10 家庭中文版安装docker ,但是没有 Hyper-V , 这样一步搞定
本人要在 win 10 上安装docker,找了安装教程,按照安装教程,第一步开启Hyper-V 虚拟机,但是发现自己电脑上没有这个选项 然后找到了这位仁兄 http://www.win7999.c ...
- matplotlib学习(2)
1.legend的学习(图例)1.1 代码 import matplotlib.pyplot as plt import numpy as np x=np.linspace(-3,3,50) #从-1 ...
- nyoj 24
素数距离问题 时间限制:3000 ms | 内存限制:65535 KB 难度:2 描述 现在给出你一些数,要求你写出一个程序,输出这些整数相邻最近的素数,并输出其相距长度.如果左右有等距离长度 ...