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就…
D1.Remove the Substring (easy version) time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard output The only difference between easy and hard versions is the length of the string. You are given a string…
D2. Remove the 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 length of the string. You are given a stri…
D2. Remove the Substring (hard version) 给字符串s,t,保证t为s的子序列,求s删掉最长多长的子串,满足t仍为s的子序列 记录t中每个字母在s中出现的最右的位置, 然后从s开头开始跑 遇到和当前t[j]相同的s[i],j++ 即使得t中相邻两个字符距离最大化 注意j跑完t了,最后一位应该为s的长度 #include<bits/stdc++.h> using namespace std; ]; ]; ]; int main() { scanf("…
题目链接:https://codeforces.com/contest/1203/problem/D2 题意: 给你S串.T串,问你最长删除多长的子串使得S串里仍然有T的子序列. 思路: 想了好久,先正着跑一下S串,记录T串每一个字符最左边在哪里,再倒着跑一下,记录T串的每一个字符最右边在哪里. 最后跑一下答案: 1. 开头和结尾特判一下,但不是max( L[1]-1 , l1-R[l2] ) , 而是对两个max( L[1]-1 , l1-L[l2]-1 ).max( R[1]-1 , l1-…
题意:给你一个模式串\(t\),现在要在主串\(s\)中删除多个子串,使得得到的\(s\)的子序列依然包含\(t\),问能删除的最长子串长度. 题解:首先,我们不难想到,我们可以选择\(s\)头部到最右边的子序列的头部和最左边的子序列的尾部到\(s\)的尾部这两个子串,除去这两个子串,我们要找的最大子串一定在子序列的头部到尾部中,即子序列中两个相邻字符位置的间隔,那么很显然,我们想让相邻的字符之间相隔最大,所以问题也就转化成了:求模式串的相邻字符在主串中的最大间隔长度,最优的情况一定是最左的子序…
这题初赛让我白给了6分,于是我决定回来解决一下它. 说实话,看原题题面和看CCF代码真是两种完全不同的感受…… ------------思路分析: 把$s$串删去一部分之后,会把$s$串分成两部分,当然其中一部分有可能为空.$t$串作为$s$串的字串,在删去一部分之后也会被分为两部分.因此我们可以枚举$t$串被分开的位置,然后进行计算. 设$t$串在$t[p]$和$t[p+1]$之间被分开,$s$串在$s[i]$和$s[i+1]$之间被分开.因为要使答案最大,因此我们要让$s$串的左半部分包含且…
D2. Remove the Substring (hard version) 思路:其实就是贪心吧,先从前往后找,找到 t 可在 s 中存在的最小位置 (pre),再从后往前找,找到 t 可在 s 中存在的最大位置(last),然后 last [ i+1 ] - pre [ i ] - 1 表示的即是 t 中两个相邻字符可以删去的最大连续字串长度,当然也不要忘记第一个字符前面删去( last [ 0 ] )的和最后末尾删去的字符串(slen - pre [ tlen - 1 ] - 1) 代码…
D2. RGB Substring (hard version) inputstandard input outputstandard output The only difference between easy and hard versions is the size of the input. You are given a string…
D1. RGB Substring (easy version) time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard output The only difference between easy and hard versions is the size of the input. You are given a string s consistin…