Codeforces Round #402 (Div. 2) D.String Game 二分搜索
来源:互联网 发布:百度大数据奏鸣曲 编辑:程序博客网 时间:2024/05/06 05:02
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.
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).
Print a single integer number, the maximum number of letters that Nastya can remove.
ababcbaabb5 3 4 1 7 6 2
3
bbbabbbb1 6 3 4 2 5
4
In the first sample test sequence of removing made by Nastya looks like this:
"ababcba" "ababcba" "ababcba" "ababcba"
Nastya can not continue, because it is impossible to get word "abb" from word "ababcba".
So, Nastya will remove only three letters.
这个题在比赛中没有想到用二分,0rzzz,但是下来一搜别人的博客是二分,就跑去把代码敲了出来,可能是自己对二分的理解还不够深刻吧。
code:
#include<cstring>
#include<cstdio>
const int MAXN=2e5+5;
char str1[MAXN],str2[MAXN];
int order[MAXN];
bool visit[MAXN];
int Len1,Len2;
bool C(int x){
memset(visit,false,sizeof(visit));
for(int i=0;i<x;++i)
visit[order[i]]=true;
int i=0,j=0;
for(;i<Len1&&j<Len2;++i){
if(!visit[i]){
if(str2[j]==str1[i])++j;
}
}
return j==Len2;
}
int main(){
scanf("%s%s",str1,str2);
Len1=strlen(str1);
Len2=strlen(str2);
for(int i=0;i<Len1;++i){
int a;scanf("%d",&a);
order[i]=a-1;
}
int lo=-1,hi=Len1+1;
while(hi-lo>1){
int mid=(hi+lo)/2;
if(C(mid))lo=mid;
else hi=mid;
}
printf("%d\n",lo);
}
- 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)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 D. String Game
- codeforces 778D. String Game (二分搜索)
- Codeforces Round #402 (Div. 2) D String Game —— 二分法
- Codeforces Round #251 (Div. 2) D 二分
- Codeforces Round #352 (Div. 2) D 二分
- Codeforces Round #377 (Div. 2) D 二分
- 腾讯云网站建立实录-ftp
- 大量插入数据时关闭主键唯一性检测
- Java中Runnable和Thread的区别
- 你需要了解JS框架
- AndroidStudio上传自己的项目到Bintray jCenter远程仓库!
- Codeforces Round #402 (Div. 2) D.String Game 二分搜索
- Android 内存泄漏总结
- 连接打印机提示:找不到驱动程序,windows在网络上找不到Canon LBP2900的驱动程序,若要手动查找,请单击“确定”。否则,请单击“取消”并咨询你的网络管理员或者访问打印机制造商的网站
- ERP系统测试用例设计
- HTTP Status 500
- 设备树学习之(一)GPIO中断
- 安装kafka以及kafka的简单运行
- Android自动化测试框架Espresso(三)——View Action的使用
- mysql 筛选数据大于等于日期不出来数据的原因
Let's consider the first test case, where you have this order of deleting characters:
Let's write down if we could obtain the target word after deleting characters order[0], order[1], ... order[i] of the original word (Note that we always delete characters in this order):
This sequence is monotonous and thus, we could do a binary search to find the maximum index, which brings "YES". :)
Hope that helps!