Codeforces 1203D2 Remove the Substring (hard version)

Topic links: https://www.luogu.org/problem/CF1203D2

Question is intended: to give you two strings s, t (length 2E5), to ensure that the sub-sequence s t is, the maximum length can inquire deleted substrings in s, and to ensure that the sub-sequence is deleted s or t

Analysis: seek first pre and last two arrays, sequences were preserved after the leftmost meet subsequence t of t meet the rightmost.

After compared in turn can

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
const int maxn=2e5+7; 
const int mod=1e9+7;
#define mem0(a) memset(a,0,sizeof(a))
#define meminf(a) memset(a,0x3f,sizeof(a))
#define ls rt<<1
#define rs rt<<1|1
#define mid (l+r)>>1
char s[maxn],t[maxn];
int pre[maxn],last[maxn];
int main(){
    scanf("%s%s",s+1,t+1);
    int id=1;
    int slen=strlen(s+1),tlen=strlen(t+1);
    for(int i=1;i<=slen;i++){
        if(s[i]==t[id]){
            pre[id]=i;
            id++;
        }
        if(id>tlen)break;
    }
    id=tlen;
    for(int i=slen;i>=1;i--){
        if(s[i]==t[id])last[id]=i,id--;
        if(id<1)break;
    }
    int ans=max(last[1]-1,slen-pre[tlen]);
    for(int i=1;i<=tlen;i++){
        ans=max(ans,last[i+1]-pre[i]-1);
    }
    printf("%d\n",ans);
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/qingjiuling/p/11618240.html