[CF1303E] Erase Subsequences - dp
Solution
不由分说地枚举分割点
令 \(f[i][j]\) 表示原串处理到 \(i\) ,\(s_1\) 处理到 \(j\),\(s_2\) 最多能处理到哪里
采用主动转移
任意情况, \(f[i][j] \to f[i+1][j]\)
如果 \(s[i+1]=s_1[j+1]\) ,那么 \(f[i][j] \to f[i+1][j+1]\)
如果 \(s[i+1]=s_2[f[i][j]+1]\) ,那么 \(f[i][j]+1 \to f[i+1][j]\)
时间复杂度 \(O(n^3)\)
#include <bits/stdc++.h>
using namespace std;
int n,m,l1,l2;
char s[405],t[405],*s1,*s2;
int f[405][405];
void sh(int x,int &y) {
y=max(x,y);
}
signed main() {
ios::sync_with_stdio(false);
int T;
cin>>T;
while(T--) {
cin>>s+1>>t+1;
n=strlen(s+1);
m=strlen(t+1);
int ans=0;
for(int d=0;d<=m;d++) {
s1=t;
s2=t+d;
l1=d;
l2=m-d;
memset(f,-0x3f,sizeof f);
f[0][0]=0;
{
int i=0,j=0;
sh(f[i][j],f[i+1][j]);
if(s[i+1]==s1[j+1]) sh(f[i][j],f[i+1][j+1]);
if(s[i+1]==s2[f[i][j]+1]) sh(f[i][j]+1,f[i+1][j]);
}
for(int i=1;i<=n;i++) {
for(int j=0;j<=min(l1,i);j++) {
sh(f[i][j],f[i+1][j]);
if(s[i+1]==s1[j+1]) sh(f[i][j],f[i+1][j+1]);
if(f[i][j]>=0 && s[i+1]==s2[f[i][j]+1]) sh(f[i][j]+1,f[i+1][j]);
}
}
if(f[n][l1]==l2) ans=1;
}
cout<<(ans>0?"YES":"NO")<<endl;
}
}
[CF1303E] Erase Subsequences - dp的更多相关文章
- Codeforces 1303E. Erase Subsequences 代码(dp 字符串压缩一维状态优化)
https://codeforces.com/contest/1303/problem/E #include<bits/stdc++.h> using namespace std; ; i ...
- codeforces 597C C. Subsequences(dp+树状数组)
题目链接: C. Subsequences time limit per test 1 second memory limit per test 256 megabytes input standar ...
- HDU 2227 Find the nondecreasing subsequences (DP+树状数组+离散化)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2227 Find the nondecreasing subsequences ...
- HDU 2227 Find the nondecreasing subsequences(DP)
Problem Description How many nondecreasing subsequences can you find in the sequence S = {s1, s2, s3 ...
- Distinct Subsequences (dp)
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- HDU 2227 Find the nondecreasing subsequences dp思想 + 树状数组
http://acm.hdu.edu.cn/showproblem.php?pid=2227 用dp[i]表示以第i个数为结尾的nondecreasing串有多少个. 那么对于每个a[i] 要去找 & ...
- Codeforces1303E. Erase Subsequences
转换一下题意,就相当于问t能不能和s中2个不相同的子串相同,我们可以将t串拆成2个子串t1,t2,得到状态dp[i][j][k]=0/1,表示s判断到i位,t1判断到j位,t2判断到k位,0/1表示是 ...
- 2021record
2021-10-14 P2577 [ZJOI2004]午餐 2021-10-13 CF815C Karen and Supermarket(小小紫题,可笑可笑) P6748 『MdOI R3』Fall ...
- 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 ...
随机推荐
- [Redis-CentOS7]Redis事务操作(六)
事务操作 隔离操作: 事务中所有的命令都会序列化,按顺序执行,不会被其他命令打扰 原子操作: 事务中所有的命令要么全部执行,要么全部不执行 添加事务并执行 127.0.0.1:6379> MUL ...
- 理解设备像素、设备独立像素、css像素、viewport
设备像素也叫物理像素. 设备像素指的是显示器上的真实像素,每个像素的大小是屏幕固有的属性,屏幕出厂以后就不会改变了. 设备分辨率描述的就是这个显示器的宽和高分别是多少个设备像素. 设备像素和设备分辨率 ...
- Explain执行计划与索引优化实践
一.何为explain执行计划? 使用explain关键字可以模拟优化器执行SQL语句,从而知道MySQL是如何使用索引来处理你的SQL查询语句以及连接表,可以分析查询语句或是结构的性能瓶颈,帮助我们 ...
- 基于S2SH开发学生考勤管理系统 附源码
开发环境: Windows操作系统开发工具:Eclipse+Jdk+Tomcat+mysql数据库 运行效果图 源码及原文链接:http://javadao.xyz/forum.php?mod=vie ...
- Kong 系列【六】添加插件---ip-restriction之黑白名单
写在前边 本地postMan请求http://192.168.130.131:8000/test-route,可以正常访问,本地IP:192.168.130.1同样在虚拟机环境192.168.130. ...
- JavaBIO利用装饰器模式来组织和扩展接口
Stream接口,它直接负责字节流的传输. Reader/Writer接口,它本身不能读直接读写数据,而是以Stream接口为内部核心,在外围装饰增强,负责字符流的读写.字符和字节的转换过程必须指定字 ...
- VAR 学习笔记3
脉冲响应图及方差分析 当使用VAR模型的时候需要完成: 选择合适的变量 就是研究变量这个没有疑问 判断滞后阶数 根据AIC和SC准则,选择 为何做格兰杰因果检验 如果给定 \(x_t\) 的滞后阶数, ...
- python进阶----深拷贝&浅拷贝
复制需要区分,,复制的是源文件的数据,还是指向源文件数据的地址 1.引用 b=a时,理解为b指向了a指向的数据,相当于引用复制 a=[1,2] b=a #验证复制的是数据还是地址 #1.查看ab的 ...
- 静态库&动态库&导入库
我遇到的问题 先贴一个StackOverflow上的问题 上面的问题让我知道了更多动态库的知识. 我需要使用一个声音库(irrKlang)为2d游戏提供声音,我使用的编译器是mingw-w64,但是i ...
- 关于vector的描述
对于有些编译器而言,使用vector<vector<int>> vec;并不能通过,必须采用vector<vector<int> >vec才可以通过.两 ...