Codeforces Round #579 (Div. 3)D2Remove the Substring (hard version)

Codeforces Round #579 (Div. 3)D2Remove the Substring (hard version)

http://codeforces.com/contest/1203/problem/D2

题目大意:给出一段长串s1和它的一段非连续子串s2,求s1最长的连续删除区间,使得s2仍为s1的非连续子串

分析:最大分割情况只可能是两种,一种是最左端或者最右端删去一大段,另一种是中间任意两个字母间删去一大段

先看第一种,那么肯定是要找到s2在s1中的最左相同点和最右相同点,很简单

另一种,删去的区间端点两个字母肯定是s2中相邻的字母,即保证删去中间一段没用的,仍是子串 例如s1:abbccbbbcccddde s2: ab
那么最长区间肯定是找到s1最左端满足条件的a,再找到s1最右端满足条件的b,中间都是可以删的,如果s2:abd 就找到s1最左端满足条件的b,再找到s1最右端满足条件的d,作差值,然后取max

以此类推

/**
 *  Author1: low-equipped w_udixixi
 *  Author2: Sherؼlock
 *  Date :2019-08-13
 **/
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<queue>
#include<map>
#define ll long long
#define pb push_back
#define rep(x,a,b) for (int x=a;x<=b;x++)
#define repp(x,a,b) for (int x=a;x<b;x++)
#define pi 3.14159265358979323846
#define mem(a,x) memset(a,x,sizeof a)
using namespace std;
const int maxn=2e5+7;
const int INF=1e9;
const ll INFF=1e18;
char s1[maxn],s2[maxn];
int fro[maxn],bac[maxn];
int main()
{
    cin>>s1;cin>>s2;
    int l1=strlen(s1),l2=strlen(s2);
    int j=0;
    for (int i=0;i<l1&&j<l2;i++)
        if (s1[i]==s2[j])
            fro[j++]=i;
    j=l2-1;
    for (int i=l1-1;i>=0&&j>=0;i--)
        if (s1[i]==s2[j])
            bac[j--]=i;
    int maxx1=max(fro[0],l1-1-fro[l2-1]);//第一种情况
    int maxx2=max(bac[0],l1-1-bac[l2-1]);//第一种情况
    int maxx=max(maxx1,maxx2);
    rep(i,1,l2-1)
        maxx=max(maxx,bac[i]-fro[i-1]-1);//第二种情况
    cout<<maxx<<endl;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值