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

题目:
D. String Game
time limit per test
2 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

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 ta1... 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.

Examples
input
ababcba
abb
5 3 4 1 7 6 2
output
3
input
bbbabb
bb
1 6 3 4 2 5
output
4
Note

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,但是下来一搜别人的博客是二分,就跑去把代码敲了出来,可能是自己对二分的理解还不够深刻吧。

 
2 months ago,  #  ^  |  Add to favourites
 Rev. 2     Vote: I like it +39 Vote: I do not like it

Let's consider the first test case, where you have this order of deleting characters:

original word: ababcba
target word: abb
order: 5 3 4 1 7 6 2.

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):

[     5,   3,   4,  1,  7,  6,  2 ]
[   YES, YES, YES, NO, NO, NO, NO ]

This sequence is monotonous and thus, we could do a binary search to find the maximum index, which brings "YES". :)

Hope that helps!


通过上面这个我们就可以很清楚的明白,二分争对的是一个单调序列,暨前面都是YES,而中间突然变成了NO,我们就是去找这个转折的地方在哪。如果符合条件(C函数)lo就是mid,否则hi就是mid.

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);
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值