【Codeforces 762 C tow strings】二分

博客围绕一个字符串问题展开,给定两个字符串A和B,需在B中删除最少连续字符使其成为A的子序列。作者提出求pre和suf数组,前者表示B中字符在A中从左到右成为子序列的最小下标,后者从右到左。通过二分起点和右端点求解,让删除长度最小。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

cf762C

• 给定两个字符串A,B
• 再B中删除最少的连续字符(一段字符),使得B成为A的子序列
• 1 ≤ |A|, |B| ≤ 1e5

这题我的想法是这样的:
首先你求一个pre数组和suf数组 pre数组代表第二个串中到目前为止如果可以在第一个串中成为子序列他的从左到右最小下标是多少
suf数组同理代表第二个串中到目前为止如果可以在第一个串中成为子序列他的从右到左最小小标是多少
那么我只要二分开始删除的起点 二分一下右端点 满足 pre[i] != -1 且 pre[i] < suf[右端点] 那么这样他们就能构造成一个子序列
我只要让删除的长度最小 就是我想要的答案

/*
    if you can't see the repay
    Why not just work step by step
    rubbish is relaxed
    to ljq
*/
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#include <vector>
#include <stdlib.h>
#include <algorithm>
using namespace std;

#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define dbg2(x1,x2) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<endl
#define dbg3(x1,x2,x3) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<" "<<#x3<<" = "<<x3<<endl
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))

typedef pair<int,int> pll;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll _INF = 0xc0c0c0c0c0c0c0c0;
const ll mod =  (int)1e9+7;

ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll ksm(ll a,ll b,ll mod){int ans=1;while(b){if(b&1) ans=(ans*a)%mod;a=(a*a)%mod;b>>=1;}return ans;}
ll inv2(ll a,ll mod){return ksm(a,mod-2,mod);}
void exgcd(ll a,ll b,ll &x,ll &y,ll &d){if(!b) {d = a;x = 1;y=0;}else{exgcd(b,a%b,y,x,d);y-=x*(a/b);}}//printf("%lld*a + %lld*b = %lld\n", x, y, d);

/*namespace sgt
{
    #define mid ((l+r)>>1)

    #undef mid
}*/
const int MAX_N = 100025;
char str[MAX_N],str_[MAX_N];
int len,len_,pre[MAX_N],suf[MAX_N];
set<int > st[30];
int main()
{
    //ios::sync_with_stdio(false);
    //freopen("a.txt","r",stdin);
    //freopen("b.txt","w",stdout);
    string ans= "";
    scanf("%s",str+1);
    scanf("%s",str_+1);
    len = strlen(str+1);
    len_ = strlen(str_+1);
    memset(pre,-1,sizeof(pre));
    memset(suf,-1,sizeof(suf));
    for(int i = 1;i<=len;++i)
    {
        st[str[i]-'a'+1].insert(i);
    }
    for(int i = 1;i<=26;++i)
    {
        if(st[i].empty()) continue;
        st[i].insert(len+1);
    }
    int now = 0;
    for(int i = 1;i<=len_;++i)
    {
        set<int>::iterator it ;
        if(st[str_[i]-'a'+1].empty()) break;
        it = st[str_[i]-'a'+1].lower_bound(now+1);
        if(it==st[str_[i]-'a'+1].end()||*it==len+1) break;
        else now = *it;
        pre[i] = now;
    }
    now = len+1;
    for(int i = len_;i>=1;--i)
    {
        set<int>::iterator it;
        if(st[str_[i]-'a'+1].empty()) break;
        it = st[str_[i]-'a'+1].upper_bound(now-1);
        if(it==st[str_[i]-'a'+1].begin()) break;
        else it--;
        now = *it;
        suf[i] = now;
    }
    int minn = len_;
    for(int i = 1;i<=len_;++i)
    {
        if(pre[i]==-1) break;
        int l = i+1,r = len_;
        while(l<=r)
        {
            int mid = (l+r)>>1;
            if(suf[mid]<=pre[i]||suf[mid]==-1) l = mid + 1;
            else r = mid - 1;
        }
        if(minn>l-i-1)
        {
            minn = l - i - 1;
            ans = "";
            for(int j = 1;j<=i;++j) ans+=str_[j];
            for(int j = l;j<=len_;++j) ans+=str_[j];
        }
    }
    for(int i = len_;i>=1;--i)
    {
        if(suf[i]==-1) break;
        int l = 1,r = i - 1;
        while(l<=r)
        {
            int mid = (l+r)>>1;
            if(pre[mid]>=suf[i]||pre[mid]==-1) r = mid - 1;
            else l = mid + 1;
        }
        if(minn>i-r-1)
        {
            minn = i - r - 1;
            ans = "";
            for(int j = 1;j<=r;++j) ans+=str_[j];
            for(int j = i;j<=len_;++j) ans+=str_[j];
        }
    }
    if(minn==len_) printf("-");
    else printf("%s",ans.c_str());
    //fclose(stdin);
    //fclose(stdout);
    //cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值