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
*********/

全部评论

相关推荐

程序员牛肉:主要是因为小厂的资金本来就很吃紧,所以更喜欢有实习经历的同学。来了就能上手。 而大厂因为钱多,实习生一天三四百的就不算事。所以愿意培养你,在面试的时候也就不在乎你有没有实习(除非是同级别大厂的实习。) 按照你的简历来看,同质化太严重了。项目也很烂大街。 要么换项目,要么考研。 你现在选择工作的话,前景不是很好了。
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
06-24 20:25
腾讯今年实习招了这么多人,后面秋招还会招人吗??想着秋招再战来着
牛客965593684号:腾讯好像2020年之后就是实习生招得多,应届生基本上不招,纯实习转正
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务