Codeforces Round #402 (Div. 2) D. String Game 二分

本文介绍了一个名为 D.StringGame 的 CodeForces 编程挑战题目,并提供了一段 C++ 代码实现。该代码通过二分查找确定了在特定字符串操作下能够匹配目标字符串的最大元素数量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

D. String Game

链接:

http://codeforces.com/contest/779/problem/D

代码 :

 1 #include <map>
 2 #include <set>
 3 #include <cmath>
 4 #include <queue>
 5 #include <stack>
 6 #include <cstdio>
 7 #include <string>
 8 #include <vector>
 9 #include <cstring>
10 #include <iostream>
11 #include <algorithm>
12 #include <functional>
13 using namespace std;
14 #define rep(i,a,n) for (int i=a;i<=n;i++)
15 #define per(i,a,n) for (int i=n;i>=a;i--)
16 #define pb push_back
17 #define mp make_pair
18 #define all(x) (x).begin(),(x).end()
19 #define fi first
20 #define se second
21 #define SZ(x) ((int)(x).size())
22 typedef vector<int> VI;
23 typedef long long ll;
24 typedef pair<int, int> PII;
25 const ll mod = 1e9 + 7;
26 const int inf = 0x3f3f3f3f;
27 const double eps = 1e-7;
28 // head
29 
30 const int maxn = 2e5 + 7;
31 int a[maxn];
32 char p[maxn], t[maxn], s[maxn];
33 int len;
34 
35 bool check(int k) {
36     rep(i, 1, len) s[i] = t[i];
37     rep(i, 1, k) s[a[i]] = ' ';
38     int j = 1;
39     rep(i, 1, len) {
40         if (s[i] == p[j]) j++;
41         if (j > strlen(p + 1)) return true;
42     }
43     return false;
44 }
45 
46 int main() {
47     scanf("%s%s", t + 1, p + 1);
48     len = strlen(t + 1);
49     rep(i, 1, len) scanf("%d", a + i);
50     int l = 0, r = len, ans = 0;
51     while (l <= r) {
52         int mid = (l + r) / 2;
53         if (check(mid))  l = mid + 1, ans = mid;
54         else r = mid - 1;
55     }
56     cout << ans << endl;
57     return 0;
58 }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值