Codeforces 778A:String Game(二分暴力)

http://codeforces.com/problemset/problem/778/A

题意:给出字符串s和字符串p,还有n个位置,每一个位置代表删除s串中的第i个字符,问最多可以删除多少个字符使得s串依旧包含p串。

思路:想到二分,以为二分做法依旧很暴力。但是别人的做法确实就是二分暴力搞啊。

枚举删除字符数,然后判断的时候如果s串包含p串,那么可以往右区间找,否则左区间找。

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 #define N 200010
 4 char s[N], p[N];
 5 int id[N], vis[N], n, m;
 6 
 7 bool check(int x) {
 8     memset(vis, 0, sizeof(vis));
 9     for(int i = 1; i <= x; i++) vis[id[i]] = 1;
10     for(int i = 1, cnt = 1; i <= n; i++) {
11         if(vis[i]) continue;
12         if(p[cnt] == s[i]) cnt++;
13         if(cnt == m + 1) return true;
14     }
15     return false;
16 }
17 
18 int main() {
19     scanf("%s %s", s + 1, p + 1);
20     n = strlen(s + 1), m = strlen(p + 1);
21     for(int i = 1; i <= n; i++) scanf("%d", &id[i]);
22     int l = 0, r = n, ans = 1;
23     while(l <= r) {
24         int mid = (l + r) >> 1;
25         if(check(mid)) {
26             ans = mid; l = mid + 1;
27         } else r = mid - 1;
28     }
29     printf("%d
", ans);
30     return 0;
31 }
原文地址:https://www.cnblogs.com/fightfordream/p/6476875.html