(二分+字符串)String Game CodeForces - 778A

附上题目链接
这题一开始没啥思路.后来偷偷百度了一下原来是二分搜步骤数…知道思路就没啥难度了.还是要多做题思路才会开阔啊…
附上code

#include <iostream>
#include <queue>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <stack>
#include <string.h>
#include <algorithm>
#include <map>
#include <vector>
using namespace std;
const int INF = 0x3f3f3f3f;
const int mn = 2e5+10;
typedef long long ll;
int t[mn];
string a,b;
int cnt;
bool judge(int n,string str){
	string c;
	for(int i=1;i<=n;++i){
		str[t[i]-1] = '.';
	}
	for(int i=0;i<cnt;++i){
		if(str[i]!='.') c+=str[i];
	}
	int len = b.length();
	int cur=0;
	for(int i=0;i<c.length();++i){
		if(b[cur] == c[i]) cur++;
	}
	if(cur == len) return true;
	return false;
}
int main() {
	cin >> a >> b;
	int x;
	while(cin >> x){
		t[++cnt] = x;
	}
	int l=1,r=cnt+1;
	int ans=1;
	while(l<r){
		int mid=(l+r)/2;
		if(judge(mid,a)){
			ans = max(mid,ans);
			l=mid+1;
		}
		else{
			r=mid;
		}
	}
	cout << ans;

	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_45590210/article/details/104230359