codeforces 779D 二分

来源:互联网 发布:windows.old在哪 编辑:程序博客网 时间:2024/05/16 08:52

String Game

time limit per test2 seconds
memory limit per test512 megabytes
inputstandard input
outputstandard 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 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.

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.

题目链接

题意:给你一串字符串s1和另一串字符串s2,l为s1字符串的长度,接下来l个操作,每次操作会删去原来字符串中的第x个字符,问最多操作几次,下一次操作后s2不再是s1中的子串(可不连续但顺序不变)

解题思路:很明显的二分法,二分操作数,若当前mid操作后s2仍旧是s1的子串,则增加操作数,否则减少操作数。

#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#include<string>#define maxn 200005using namespace std;char st1[maxn],st2[maxn],st3[maxn],st4[maxn];int a[maxn],ans;int main(){    scanf("%s%s",st1,st2);    int len1=strlen(st1),len2=strlen(st2);    for(int i=0;i<len1;i++) scanf("%d",&a[i]);    int l=0,r=len1,flag;    while(l<r){        flag=0;        int mid=(l+r)>>1;        strcpy(st3,st1);        for(int i=0;i<mid;i++){            st3[a[i]-1]='0';        }        int pl=0,s1=0,s2=0;        for(int i=0;i<len1;i++){            if(st3[i]!='0') st4[pl++]=st3[i];        }        while(s1<pl){            if(st4[s1]==st2[s2])    s2++;            if(s2==len2){                ans=mid;                flag=1;                break;            }            s1++;        }        if(flag)    l=mid+1;        else r=mid;    }    printf("%d\n",ans);    return 0;}

我比较喜欢在二分的时候就把符合条件的答案放入ans中,这样就可以不用考虑l,r两个数最后到底取哪一个了,节约了不必要的麻烦。

0 0
原创粉丝点击