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

题意:给定两个字符串str1和str2,再给定一个strlen(str1)的整数序列,整数i表示从str1中删除位置处于i上的字符(即一次删除操作),删除后下标不变。问最多执行多少次删除操作,使剩下的字符串依然可以通过删除字符变成str2.

妈蛋啊,比赛时弄死没想到啊。。。注意力全放在了两个字符串上。。。结果序列才是关键啊。。。

思路:二分整数序列,然后找删除后是否合法。。。就这么简单。。。继续努力。。。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
#include<stdlib.h>
using namespace std;
#define N 200005

char str1[N],str2[N];
int num[N];
bool del[N];

int main()
{
    scanf("%s%s",str1+1,str2+1);
    int len1=strlen(str1+1),len2=strlen(str2+1);
    for(int i=1;i<=len1;i++)
        scanf("%d",&num[i]);
    int l=1,r=len1,res=0;
    while(l<=r)
    {
        int mid=(l+r)>>1;
        memset(del,0,sizeof(del));
        for(int i=1;i<=mid;i++)
            del[num[i]]=1;
        int pos=1;
        for(int i=1;i<=len1;i++)
        {
            if(del[i]==1)
                continue;
            if(pos>len2)
                break;
            if(str1[i]==str2[pos])
                pos++;
        }
        if(pos>len2)
        {
            res=mid;
            l=mid+1;
        }
        else
            r=mid-1;
    }
    printf("%d
",res);
    return 0;
}
原文地址:https://www.cnblogs.com/jasonlixuetao/p/6451832.html