题解 CF1203D2 【Remove the Substring (hard version)】

题目链接

Solution CF1203D

题目大意:给定两个字符串\(s\),\(t\), \(t\)是\(s\)的子序列,问最长可以在\(s\)里面删掉多长的连续子序列使得\(t\)仍为\(s\)的子序列

贪心



分析:

假如我们\(t\)的一个前缀要匹配\(s\)的话,显然尽可能往前匹配,这样可以使得答案尽量大,后缀同理

我们用\(suf[i]\)表示\(t[1\dots i]\)可以匹配\(s\)前缀的最前的位置,\(suf[i]\)表示\(t[i \dots |t|]\)可以匹配\(s\)后缀的最后的位置

我们枚举所有\(t\)的位置\(i\),当\(pre[i] < suf[i + 1]\)时,\(suf[i + 1] - pre[i] - 1\)可以成为答案,取个最大值即可

CCF毒瘤程序

#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn = 2e5 + 100;
char s[maxn],t[maxn];
int slen,tlen,ans,pre[maxn],suf[maxn];
int main(){
    scanf("%s",s + 1);
    scanf("%s",t + 1);
    slen = strlen(s + 1);
    tlen = strlen(t + 1);
    for(int i = 1;i <= tlen;i++){
        pre[i] = pre[i - 1] + 1;
        while(pre[i] <= slen && s[pre[i]] != t[i])pre[i]++;
    }
    suf[tlen + 1] = slen + 1;
    for(int i = tlen;i >= 1;i--){
        suf[i] = suf[i + 1] - 1;
        while(suf[i] >= 1 && s[suf[i]] != t[i])suf[i]--;
    }
    for(int i = 0;i <= tlen;i++)
        if(pre[i] < suf[i + 1])ans = max(ans,suf[i + 1] - pre[i] - 1);
    printf("%d\n",ans);
    return 0-0;
}

原文地址:https://www.cnblogs.com/colazcy/p/11737255.html

时间: 2024-08-30 17:50:05

题解 CF1203D2 【Remove the Substring (hard version)】的相关文章

Codeforces 1203D2 Remove the Substring (hard version)

题目链接:https://www.luogu.org/problem/CF1203D2 题意:给你两个字符串s,t(长度为2e5),保证t是s的子序列,求问最大能在s中删子串的长度,且保证删后t还是s的子序列 分析:先求pre和last两个数组,分别保存最左边的满足t的子序列后最右边的满足t的子序列. 之后依次比较即可 #include<bits/stdc++.h> using namespace std; typedef long long ll; const int inf=0x3f3f3

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就

leetcode 题解:Remove Duplicates from Sorted Array II(已排序数组去三次及以上重复元素)

题目: Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For example,Given sorted array A = [1,1,1,2,2,3], Your function should return length = 5, and A is now [1,1,2,2,3]. 说明: 1)设个标志可实现 实现: 1 class Solution { 2 public

leetcode 题解:Remove Duplicates from Sorted Array(已排序数组去重)

题目: Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this in place with constant memory. For example,Given input array A

【Codeforces Round #575 (Div. 3) 】 RGB Substring (hard version) ( FFT)

D2. RGB Substring (hard version) time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output The only difference between easy and hard versions is the size of the input. You are given a string ss cons

D2. RGB Substring (hard version)||D1. RGB Substring (easy version)

D2. RGB Substring (hard version)    原题传送门 time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output The only difference between easy and hard versions is the size of the input. You are given a strin

Codeforces Round #575 (Div. 3) D1. RGB Substring (easy version)

Codeforces Round #575 (Div. 3) D1 - RGB Substring (easy version) The only difference between easy and hard versions is the size of the input. You are given a string s consisting of n characters, each character is 'R', 'G' or 'B'. You are also given a

LeetCode题解 #5 Longest Palindromic Substring

Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring. 在给定一个字符串S中,找到最长的回文串. 很恶心的一道题,不过应该都能想到枚举,从第一个开始枚举.枚举的字母向两头延伸,如果相同则继续,不同则开始下一

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

传送门 题意: 给你一个长为n的仅由'R','G','B'构成的字符串s,你需要在其中找出来一个子串.使得这个子串在“RGBRGBRGBRGB........(以RGB为循环节,我们称这个串为str)”里面也是一个子串,这个子串的长度是k 可是有可能s字符串中找不到,那么这个时候就可以改变s字符串中某些位置的字母来完成任务.问最少需要改变多少个字母 题解: 主要看暴力的姿势对不对.在上一道的D1上面,我是对s字符串的每一个位置进行‘R’,‘G’,‘B’的枚举,因为如果这个子串也是str的子串的话