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) - 双指针的更多相关文章

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

  2. D2. Remove the Substring (hard version)(思维 )

    D2. Remove the Substring (hard version) time limit per test 2 seconds memory limit per test 256 mega ...

  3. D2. Remove the Substring (hard version)

    D2. Remove the Substring (hard version) 给字符串s,t,保证t为s的子序列,求s删掉最长多长的子串,满足t仍为s的子序列 记录t中每个字母在s中出现的最右的位置 ...

  4. 双指针(最大删除子串)Codeforces Round #579 (Div. 3)--Remove the Substring (hard version)

    题目链接:https://codeforces.com/contest/1203/problem/D2 题意: 给你S串.T串,问你最长删除多长的子串使得S串里仍然有T的子序列. 思路: 想了好久,先 ...

  5. Codeforces Round #579 (Div. 3) D2. Remove the Substring (hard version) (思维,贪心)

    题意:给你一个模式串\(t\),现在要在主串\(s\)中删除多个子串,使得得到的\(s\)的子序列依然包含\(t\),问能删除的最长子串长度. 题解:首先,我们不难想到,我们可以选择\(s\)头部到最 ...

  6. CF1203D2 Remove the Substring (hard version) 题解

    这题初赛让我白给了6分,于是我决定回来解决一下它. 说实话,看原题题面和看CCF代码真是两种完全不同的感受…… ------------思路分析: 把$s$串删去一部分之后,会把$s$串分成两部分,当 ...

  7. Remove the Substring

    D2. Remove the Substring (hard version) 思路:其实就是贪心吧,先从前往后找,找到 t 可在 s 中存在的最小位置 (pre),再从后往前找,找到 t 可在 s ...

  8. Codeforces Round #575 (Div. 3) D2. RGB Substring (hard version) 水题

    D2. RGB Substring (hard version) inputstandard input outputstandard output The only difference betwe ...

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

随机推荐

  1. Linux压缩工具

    一.gzip/gunzip/zcat gzip, gunzip, zcat - compress or expand files gzip [ option .... ] [ filenames .. ...

  2. Socket编程-基础使用

    最后更新:2019-10-25 一 基本概念 socket, 又称为"套接字"或者"插座". 是操作系统提供的一种进程间通信机制.目前大多用于不同网络设备之间的 ...

  3. plt.plot() 无法使用参数ax

    问题参考 TypeError: inner() got multiple values for keyword argument 'ax' fig, ax=plt.subplots(2,1) plt. ...

  4. Struts1与Struts2区别?

    (1)Struts1执行过程: <1>Web容器启动的时候ActionServlet被初始化,加载struts-config.xml配置文件. <2>浏览器发送请求到Actio ...

  5. Spring的jsp标签库

    1,主要有两个 一个用于渲染HTML表单标签, 这些标签会绑定model中的某个属性 另一个包换了一些工具类标签 2,将表单绑定到模型上 包含了14个标签,海报一个人为用户展现错误的标签,它会将错误信 ...

  6. 第五周学习总结&实验报告三

    第五周课程总结 1.this和super的区别: this:访问本类中的属性,如果本类没有此属性则从父类中继续查找:访问本类中的方法,如果本类中没有此方法则从父类中继续查找:调用本类构造,必须放在构造 ...

  7. win7环境下mongodb分片和移除

    本文主要介绍在一台win7电脑上模拟mongo分片.如果有多台服务器,可以将每个mongo部署在单台电脑上.我们将配置3个mongo分片,3个配置服务器,1个路由服务器.如下图所示进行配置,介绍如何增 ...

  8. mesh之孔洞检测

    mesh之孔洞检测 图1 检测孔洞点 图2 检测孔洞点 图3 检测孔洞点 图4 细节

  9. C++ Vector实践

    实践如下: #include <iostream> #include <vector> #include <typeinfo> using namespace st ...

  10. 常用IDE 教程(IntelliJ IDEA、Android Studio、Chrome)

    1.IntelliJ IDEA 使用教程 http://wiki.jikexueyuan.com/project/intellij-idea-tutorial/ 2.Chrome 开发工具指南 htt ...