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 ...
随机推荐
- C语言写数据库(一)
/*** connect.c ***/ #include<stdio.h> #include<stdlib.h> #include"mysql.h" int ...
- Java 工程师成神之路
基础篇 → 什么是面向对象 面向对象.面向过程 是一种新兴的程序设计方法,或者是一种新的程序设计规范(paradigm),其基本思想是使用对象.类.继承.封装.多态等基本概念来进行程序设计.从现实世界 ...
- HDU 6089 Rikka with Terrorist (线段树)
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=6089 题解 这波强行维护搞得我很懵逼... 扫描线,只考虑每个点能走到左上方(不包括正上方,但包括正左 ...
- webpack学习之路--demo1
1.不使用框架建立webpack项目时 (1).npm init -y 生成package.json文件 (2).npm install --save-dev webpack 在当前项目下安装webp ...
- pom文件中的dependencyManagement和dependencies的区别
dependencies 子项目中,自动继承父项目中的相关依赖 dependencyManagement 只是声明依赖,并不实现引入,因此子项目中需要显示的声明需要用的依赖.如果不在子项目中声明依赖, ...
- ffmpeg循环推流
ffmpeg循环推流 有时候需要轮播出一路直播 这个时候循环推流就比较方便了 ffmpeg -stream_loop - -re -i d:/Media/a.ts -vcodec h264 -acod ...
- Mysql 基础操作命令
1,查看mysql的建表语句 show create table tableName; #tableName 库中已存在的表名
- leetcode-easy-string-242. Valid Anagram
mycode 71.97% class Solution(object): def isAnagram(self, s, t): """ :type s: str : ...
- c++11多线程---线程锁(mutex)
#include<mutex> 包含四类锁: 1 std::mutex 最基本也是最常用的互斥类 2 std::recursive_mutex 同一线程内可递归 ...
- 通过实例聊聊Java中的多态
Java中的多态允许父类指针指向子类实例.如:Father obj=new Child(); 那么不禁要发问?? 使用这个父类型的指针访问类的属性或方法时,如果父类和子类都有这个名称的属性或方法,哪 ...