C. Maximum width (thinking + greedy)

https://codeforces.com/contest/1492/problem/C


Ideas:

It's good to be greedy after maintaining the desirable range of the second string.

#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=4e5+100;
typedef long long LL;
inline LL read(){LL x=0,f=1;char ch=getchar();	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;}
LL a[maxn],b[maxn];
char s[maxn],t[maxn];
int main(void)
{
  cin.tie(0);std::ios::sync_with_stdio(false);
  LL n,m;cin>>n>>m;
  cin>>(s+1)>>(t+1);
  LL len1=m;LL len2=1;
  for(LL i=n;i>=1;i--){
    if(t[len1]==s[i]){
        a[len1]=i;
        --len1;
        if(0>len1) break;
    }
  }
  for(LL i=1;i<=n;i++){
    if(t[len2]==s[i]){
        b[len2]=i;
        ++len2;
        if(m<len2) break;
    }
  }
  LL res=0;
  for(LL i=1;i<=m;i++) res=max(res,a[i+1]-b[i]);
  cout<<res<<"\n";
return 0;
}

 

Guess you like

Origin blog.csdn.net/zstuyyyyccccbbbb/article/details/114638539