专栏文章
题解:CF1203D1 Remove the Substring (easy version)
CF1203D1题解参与者 2已保存评论 1
文章操作
快速查看文章及其快照的属性,并进行相关操作。
- 当前评论
- 1 条
- 当前快照
- 1 份
- 快照标识符
- @mipzmb7v
- 此快照首次捕获于
- 2025/12/03 20:32 3 个月前
- 此快照最后确认于
- 2025/12/03 20:32 3 个月前
题目大意
给定两个字符串 和 ,其中 是 的子序列。要求删除 中的一个连续子串,使得删除后的 仍包含 作为子序列。求可删除的最长连续子串的长度。
解题思路
预处理左匹配位置:
构造数组 ,其中 表示 的前 个字符在 中最左匹配的结束位置。
预处理右匹配位置:
构造数组 ,其中 表示 的后 个字符在 中最右匹配的起始位置。
计算最大间隙:
遍历所有可能的间隙 ,计算 的最大值,即为可删除的最长子串长度。
附上代码
CPP#include<bits/stdc++.h>
using namespace std;
string s,t;
int left[1000005],right[1000005];
int main(){
cin>>s>>t;
memset(left,-1,sizeof(left));
memset(right,s.size(),sizeof(right));
int n=t.size();
left[0]=-1;
for(int i=1;i<=n;++i){
char c=t[i-1];
int pos=left[i-1]+1;
while(pos<s.size()&&s[pos]!=c){
pos++;
}
left[i]=pos;
}
right[n]=s.size();
for(int i=n-1;i>=0;--i){
char c=t[i];
int pos=right[i+1]-1;
while(pos>=0&&s[pos]!=c){
pos--;
}
right[i]=pos;
}
int maxx=0;
for(int i=0;i<=n;++i){
int current=right[i]-left[i]-1;
if(current>maxx){
maxx=current;
}
}
cout<<maxx;
return 0;
}
相关推荐
评论
共 1 条评论,欢迎与作者交流。
正在加载评论...