Codeforces - 1203D2 - Remove the Substring (hard version) - 双指针
https://codeforces.com/contest/1203/problem/D2
上次学了双指针求两个字符串之间的是否t是s的子序列。但其实这个双指针可以求出的是s的前i个位置中匹配t的最长的前缀。反过来求一次可以得到最长的后缀。
然后怎么找要删除的位置呢?暴力n^2肯定可以,然后线性写挂到自闭。
枚举位置[i,j),注意j可以取相等,所以预处理前后缀的时候把n位置的后缀也算好。
去除子串[i,j),那么剩下的就是[0,i-1]和[j,n-1]两个子串,他们匹配的长度加起来超过tl就是合法。
#include<bits/stdc++.h>
using namespace std;
int dpprefix[200005];
int dpsuffix[200005];
char s[200005], t[200005];
int sl, tl;
void prefix() {
int i = 0, j = 0;
while(i < sl || j < tl) {
if(i < sl && j < tl) {
if(s[i] == t[j]) {
dpprefix[i] = j + 1;
++i, ++j;
} else {
dpprefix[i] = j;
++i;
}
} else if(j == tl) {
dpprefix[i] = tl;
++i;
} else if(i == sl) {
dpprefix[i] = j;
break;
}
}
}
void suffix() {
int i = sl - 1, j = tl - 1;
while(i >= 0 || j >= 0) {
if(i >= 0 && j >= 0) {
if(s[i] == t[j]) {
dpsuffix[i] = tl - j;
--i, --j;
} else {
dpsuffix[i] = tl - (j + 1);
--i;
}
} else if(j < 0) {
dpsuffix[i] = tl;
--i;
} else if(i < 0) {
dpprefix[i] = tl - j;
break;
}
}
}
int main() {
#ifdef Yinku
freopen("Yinku.in", "r", stdin);
#endif // Yinku
while(~scanf("%s%s", s, t)) {
memset(dpprefix, 0, sizeof(dpprefix));
memset(dpsuffix, 0, sizeof(dpsuffix));
sl = strlen(s), tl = strlen(t);
prefix();
suffix();
int i = 0, j = 1;
int ans = 0;
while(i < sl || j <= sl) {
if(i == 0) {
while(j <= sl && dpsuffix[j] >= tl) {
ans = max(ans, j - i);
++j;
}
} else {
while(j <= sl && dpprefix[i - 1] + dpsuffix[j ] >= tl) {
ans = max(ans, j - i);
++j;
}
}
++i;
}
printf("%d\n", ans);
}
return 0;
}
Codeforces - 1203D2 - Remove the Substring (hard version) - 双指针的更多相关文章
- CF #579 (Div. 3) D1.Remove the Substring (easy version)
D1.Remove the Substring (easy version) time limit per test2 seconds memory limit per test256 megabyt ...
- D2. Remove the Substring (hard version)(思维 )
D2. Remove the Substring (hard version) time limit per test 2 seconds memory limit per test 256 mega ...
- D2. Remove the Substring (hard version)
D2. Remove the Substring (hard version) 给字符串s,t,保证t为s的子序列,求s删掉最长多长的子串,满足t仍为s的子序列 记录t中每个字母在s中出现的最右的位置 ...
- 双指针(最大删除子串)Codeforces Round #579 (Div. 3)--Remove the Substring (hard version)
题目链接:https://codeforces.com/contest/1203/problem/D2 题意: 给你S串.T串,问你最长删除多长的子串使得S串里仍然有T的子序列. 思路: 想了好久,先 ...
- Codeforces Round #579 (Div. 3) D2. Remove the Substring (hard version) (思维,贪心)
题意:给你一个模式串\(t\),现在要在主串\(s\)中删除多个子串,使得得到的\(s\)的子序列依然包含\(t\),问能删除的最长子串长度. 题解:首先,我们不难想到,我们可以选择\(s\)头部到最 ...
- CF1203D2 Remove the Substring (hard version) 题解
这题初赛让我白给了6分,于是我决定回来解决一下它. 说实话,看原题题面和看CCF代码真是两种完全不同的感受…… ------------思路分析: 把$s$串删去一部分之后,会把$s$串分成两部分,当 ...
- Remove the Substring
D2. Remove the Substring (hard version) 思路:其实就是贪心吧,先从前往后找,找到 t 可在 s 中存在的最小位置 (pre),再从后往前找,找到 t 可在 s ...
- Codeforces Round #575 (Div. 3) D2. RGB Substring (hard version) 水题
D2. RGB Substring (hard version) inputstandard input outputstandard output The only difference betwe ...
- Codeforces Round #575 (Div. 3) D1+D2. RGB Substring (easy version) D2. RGB Substring (hard version) (思维,枚举,前缀和)
D1. RGB Substring (easy version) time limit per test2 seconds memory limit per test256 megabytes inp ...
随机推荐
- idea2018.3.6安装与破解教程(亲测可用、破解到2100年)
最近,帮室友进行idea安装,之前自己安装借鉴的博客已404,在网上找了好几个都无效,想着总结一份备用. 此博客是又找了一台电脑,边安装边写的. 目录 (已安装好的,可以直接看idea2018.3.6 ...
- [LOJ3106][TJOI2019]唱、跳、rap和篮球:DP+生成函数+NTT+容斥原理
分析 令\(f(i)\)表示共\(i\)组同学讨论cxk的位置的方案数(不考虑其他位置上的人的爱好),这个数组可以很容易地通过依次考虑每个位置是否是四个人中最后一个人的位置来递推求解,时间复杂度\(O ...
- windows基础提权
Window基础提权 提到system权限 甚至让他变成你的肉鸡 我们了解一下windows下面有那些用户 Guests是用户最低的权限 而且一般是被禁用的 User权限也很低 连关机都不行 还有wi ...
- Zookeeper 选举过程
Zookeeper 选举过程 问题 选举过程 服务器之间是怎么通信的? 答:QuorumCnxManager使用TCP-socket实现选举过程中的连接通信 Leader的选举过程在什么时候实现? L ...
- 事件源event.target
今天在了解检测浏览器是否能播放不同类型的视频时发现以下代码 <script> function supportType(e,vidType,codType) { myVid=documen ...
- EPPlus生成Excel表格(只支持2007及以上)
主要来源: https://www.cnblogs.com/rumeng/p/3785748.html http://epplus.codeplex.com/ FileInfo newFile = n ...
- java 传值
好文章:https://zwmf.iteye.com/blog/1738574 public class Test { public int i,j; public void test_m(Test ...
- BOSCH汽车工程手册————混合驱动
首先放一波资源,一千两百多页的pdf 链接:https://pan.baidu.com/s/15IsvHqOFCnqAKwY_SR4-lA提取码:6wmz 混合驱动 混合驱动有串联驱动并联驱动以及两种 ...
- np.hstack和np.vstack
np.vstack:按垂直方向(行顺序)堆叠数组构成一个新的数组 In[3]: import numpy as np In[4]: a = np.array([[1,2,3]]) a.shape Ou ...
- zk 文件存储
zk 有 2 种文件,快照和事务日志,快照是某一时刻的全量数据,事务日志中记录了数据的修改事件. 快照的文件名是 snapshot.zxid,zxid 是当前最大的事务 id // org.apach ...