Codeforces Round #402 (Div. 2) D. String Game[二分]

链接:传送门

题意:有p,t两个字符串,现在有p.size()次操作,问最多能执行多少次操作,使得t仍旧是p的子序列.一开始t一定是p的子序列

思路:二分check

#include<bits/stdc++.h>
#define PI acos(-1.0)
#define pb push_back
#define F first
#define S second
#define debug puts
using namespace std;
typedef long long ll;

const int N=2e5+5;
const int MOD=1e9+7;
const int INF=0x3f3f3f3f;
string p,t;
int a[N],sz;

bool check(int st){
    vector<int> vec;
    for(int i=st;i<sz;i++)  vec.pb(a[i]-1);
    sort(vec.begin(),vec.end());
    string tt="";
    for(auto i:vec) tt+=p[i];
//    cout << tt << endl;
    int i,j;
    for(i=0,j=0;i<(int)tt.size();i++){
        if(j<(int)t.size()&&tt[i]==t[j])    j++;
    }
    return j==(int)t.size();
}

int main(void){
    cin >> p >> t;
    sz=(int)p.size();
    for(int i=0;i<sz;i++)  scanf("%d",&a[i]);
    int l=0,r=sz-1,ans;
    while(l<=r){
        int mid=l+r>>1;
//        cout <<"mid="<<mid<<" ";
        if(check(mid))  l=mid+1,ans=mid;
        else    r=mid-1;
    }
    cout << ans << endl;

    return 0;
}
void bef0re_submit(){
    debug("Make sure the algorithm is right!");
    debug("LONG LONG!!!");
    debug("Check n and m are misuesd???");
    debug("Make sure output format is right/// Yes??YES  (%.20LF)???");
    debug("if all meet,run with special situation!!!");
    debug("I confirm that I have done all above");
}


/*********
4 5
0 3 8 4
*********/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值