【Codeforces Round #402 (Div. 1)】Codeforces 778A String Game
来源:互联网 发布:网络编程培训 百度云 编辑:程序博客网 时间:2024/05/22 01:56
Little Nastya has a hobby, she likes to remove some letters from word,
to obtain another word. But it turns out to be pretty hard for her,
because she is too young. Therefore, her brother Sergey always helps
her. Sergey gives Nastya the word t and wants to get the word p out of
it. Nastya removes letters in a certain order (one after another, in
this order strictly), which is specified by permutation of letters’
indices of the word t: a1… a|t|. We denote the length of word x as
|x|. Note that after removing one letter, the indices of other letters
don’t change. For example, if t = “nastya” and a = [4, 1, 5, 3, 2, 6]
then removals make the following sequence of words “nastya” “nastya”
“nastya” “nastya” “nastya” “nastya” “nastya”. Sergey knows this
permutation. His goal is to stop his sister at some point and continue
removing by himself to get the word p. Since Nastya likes this
activity, Sergey wants to stop her as late as possible. Your task is
to determine, how many letters Nastya can remove before she will be
stopped by Sergey. It is guaranteed that the word p can be obtained by
removing the letters from word t. Input The first and second lines of
the input contain the words t and p, respectively. Words are composed
of lowercase letters of the Latin alphabet (1 ≤ |p| < |t| ≤ 200 000).
It is guaranteed that the word p can be obtained by removing the
letters from word t. Next line contains a permutation
a1, a2, …, a|t| of letter indices that specifies the order in which
Nastya removes letters of t (1 ≤ ai ≤ |t|, all ai are distinct).
Output Print a single integer number, the maximum number of letters
that Nastya can remove.
首先考虑如何判断一个字符串能否通过去掉一些字符变成另一个。显然只要
这样套上二分答案就可以了,复杂度
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;char t[200010],p[200010];int flag[200010],a[200010],n,m;bool ok(int x){ int j=1; for (int i=1;i<=x;i++) flag[a[i]]=0; for (int i=x+1;i<=n;i++) flag[a[i]]=1; for (int i=1;i<=n&&j<=m;i++) if (flag[i]&&t[i]==p[j]) j++; return j==m+1;}int main(){ int l,r,mid; scanf("%s%s",t+1,p+1); n=strlen(t+1); m=strlen(p+1); for (int i=1;i<=n;i++) scanf("%d",&a[i]); l=0,r=n; while (l<r) { mid=(l+r+1)/2; if (ok(mid)) l=mid; else r=mid-1; } printf("%d\n",l);}
- 【Codeforces Round #402 (Div. 1)】Codeforces 778A String Game
- Codeforces Round #402 (Div. 1) A. String Game
- Codeforces Round #402 (Div. 2) D. String Game (二分)
- Codeforces Round #402 (Div. 2)D. String Game 二分
- Codeforces Round #402 (Div. 2) D. String Game
- Codeforces Round #402 (div. 2)D. String Game(二分)
- Codeforces Round #402 (Div. 2)D. String Game【二分】
- Codeforces Round #402 (Div. 2) String Game 二分
- Codeforces Round #402 (Div. 2)D. String Game【二分】
- Codeforces Round #402 (Div. 2) D. String Game
- Codeforces Round #402 (Div. 2) D. String Game(二分)
- Codeforces Round #402 (Div. 2) D.String Game 二分搜索
- Codeforces Round #402 (Div. 2) D. String Game
- Codeforces Round #258 (Div. 2)1A Game With Sticks
- 【Codeforces Round #268 (Div. 1)】A. 24 Game【归纳法】
- Codeforces Round #426 (Div. 1) A. The Meaningless Game
- Codeforces 778A-String Game
- Codeforces 778A String Game
- bzoj1922 [Sdoi2010]大陆争霸 有限制最短路
- HashMap源码解析
- 自定义view-使用xml控制界面的呈现
- 2.26
- 80. Remove Duplicates from Sorted Array II
- 【Codeforces Round #402 (Div. 1)】Codeforces 778A String Game
- html5学习--js语句在html文档中的位置
- 配置Eclipse支持JDK8
- Java的基础语法
- 图像拼接与图像分割之实际应用
- 配置方法文件网址
- leetcode 刷题记录
- hdu 5724 Chess(sg博弈)
- 文章标题