题解:CF1203D1 Remove the Substring (easy version)
题目大意
给定两个字符串
解题思路
预处理左匹配位置:
构造数组
预处理右匹配位置:
构造数组
计算最大间隙:
遍历所有可能的间隙
附上代码
#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;
}