CF-#402D String Game(二分+优先队列)

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.

 题意:给出两组字符串和一组序列,然后按序列a[1]~a[k]删除第一个字符串,求第二个字符串是第一个字符串的子串时k的最大值?

思路:用二分0~len的值mid,将mid~len中的数组值放入优先队列中,然后进行检验,直到得到最大的k值。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 #include <queue>
 6 using namespace std;
 7 priority_queue<int,vector<int>,greater<int> >que;
 8 const int maxn=200005;
 9 char p[maxn],t[maxn];
10 int a[maxn];
11 int n,len,len1;
12 int cheak(int mid)
13 {
14     while(!que.empty())
15         que.pop();
16     for(int i=mid; i<len; i++)
17         que.push(a[i]);
18     int flag=0,cnt=0;
19     while(!que.empty())
20     {
21         if(p[que.top()-1]==t[cnt])
22             cnt++;
23         que.pop();
24         if(cnt==len1)
25         {
26             flag=1;
27             break;
28         }
29     }
30     return flag;
31 }
32 int main()
33 {
34     while(~scanf("%s%s",p,t))
35     {
36         len=strlen(p);
37         len1=strlen(t);
38         for(int i=0; i<len; i++)
39             scanf("%d",&a[i]);
40         int l=0,r=len,ans=0;
41         while(l<=r)
42         {
43             int mid=(l+r)/2;
44             if(cheak(mid))
45             {
46                 l=mid+1;
47                 ans=mid;
48             }
49             else r=mid-1;
50         }
51         printf("%d\n",ans);
52     }
53     return 0;
54 }
View Code

 

posted @ 2017-03-06 20:35  Wally的博客  阅读(234)  评论(0编辑  收藏  举报