C. Maximum width(思维+贪心)

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


思路:

维护一下第二个串的可取范围之后贪心就好了。

#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;
}

猜你喜欢

转载自blog.csdn.net/zstuyyyyccccbbbb/article/details/114638539