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 ...
随机推荐
- ABP core2.2错误笔记,持续更新
注:以下问题全部基于版本 © 2019 MLCDZ. Version 4.3.0.0 [20190830] .net core 的版本为2.2 1.System.InvalidOperationEx ...
- JS框架_(JQuery.js)带阴影贴纸标签按钮
百度云盘 传送门 密码:azo6 纯CSS带阴影贴纸标签按钮效果: <!doctype html> <html> <head> <meta charset=& ...
- 在mac中,npm安装或者卸载失败,提示没有权限
在终端输入 sudo chown -R $USER /usr/local 输入开机密码
- windows 简单实用Elasticsearch
官网下载地址:https://download.elasticsearch.org/elasticsearch/release/org/elasticsearch/distribution/zip/e ...
- 第八周总结&实验报告六
总结:这周主要是学习异常的使用,已经怎么解决异常,总的来说学习的还是有点难度的,因为前面的知识还需要时间去弥补,这门课程还是要加油的学习! 这周主要所学: 一.异常 1.异常是导致程序中断运行的一种指 ...
- 利用python的图像分块与拼接
import os import matplotlib.pyplot as plt import cv2 import numpy as np def divide_img(img_path, img ...
- VS2017 中安装SVN
VS2017 中安装SVN 1.下载:SVN For Vs2017 2.安装: 先关闭VS2017,找到下载文件,直接双击,安装. 3.启用插件 打开Vs2017,直接可用.
- 阶段3 3.SpringMVC·_01.SpringMVC概述及入门案例_03.入门程序之需求分析
- 工具栏对象GUI Status 与GUI Title
GUI Status 与GUI Title用于自定义工具栏按钮及Report程序标题栏显示内容, 可以通过se41\SE80或直接SE38中展开对象列表进行相关操作. 如下是在SE38里,点击[显示物 ...
- Day06:抽象类、接口和内部类(上)
JVAV中的常量 什么是常量? 常量就是不会变化的数值 为什么需要常量? 方便使用(调用)不会变化的数值 特性 不能修改 所有对象共享 常量一定是成员 定义 public static final 类 ...