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\n", ans);
30     return 0;
31 }

 

转载于:https://www.cnblogs.com/fightfordream/p/6476875.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值