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. BZOJ 1920 Luogu P4217 [CTSC2010]产品销售 (模拟费用流、线段树)

    题目链接 (bzoj) https://www.lydsy.com/JudgeOnline/problem.php?id=1920 (luogu) https://www.luogu.org/prob ...

  2. BZOJ 3836 Codeforces 280D k-Maximum Subsequence Sum (模拟费用流、线段树)

    题目链接 (BZOJ) https://www.lydsy.com/JudgeOnline/problem.php?id=3836 (Codeforces) http://codeforces.com ...

  3. 高效的js原生代码

    1.遍历元素 //不推荐 var element = document.getElementsByTagName('div'); for(var i=0; i<element.length; i ...

  4. linux iconv文件编码转换

    linux系统里提供的文件转化编码的命令iconv,例如: iconv -t utf-8 -f gb2312 -c test.xml > text_UTF8.xml 1 -f 源编码-t 目标编 ...

  5. C# base64 加密解密

    1.base64  to  string string strPath = "aHR0cDovLzIwMy44MS4yOS40Njo1NTU3L1 9iYWlkdS9yaW5ncy9taWR ...

  6. tensorflow实现迁移学习

    此例程出自<TensorFlow实战Google深度学习框架>6.5.2小节 卷积神经网络迁移学习. 数据集来自http://download.tensorflow.org/example ...

  7. 百度echars 插件 横坐标信息倾斜显示

    只需要 在xAxis   中加入 axisLabel:{                                  interval:0,//横轴信息全部显示                  ...

  8. hdjs---1、hdjs爬坑杂记

    hdjs---1.hdjs爬坑杂记 一.总结 一句话总结: 对hdjs这种文档和完善都不是很好的插件,应该先在项目的空页面试,成功后再用到用了框架的项目中 1.hdjs4.0.18引入select2? ...

  9. 环信及时通讯 Laravel 扩展包

    laravel-easemob 环信及时通讯 laravel 包开发,用于环信用户.群.聊天室等功能 github 地址   安装 加载包 "link1st/laravel-easemob& ...

  10. asp.net 获取服务器及客户端的相关信息

    1. 在ASP.NET中专用属性:获取服务器电脑名:Page.Server.ManchineName获取用户信息:Page.User获取客户端电脑名:Page.Request.UserHostName ...