码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2017-02-28 00:54:54      阅读:634      评论:0      收藏:0      [点我收藏+]

标签:int   char   ref   枚举   std   problems   memset   字符串   位置   

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 }

 

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

标签:int   char   ref   枚举   std   problems   memset   字符串   位置   

原文地址:http://www.cnblogs.com/fightfordream/p/6476875.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!