CodeForces - 779D 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"  "nasya"  "asya"  "sya"  "sa"  "a"  "".

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
Copy
ababcba
abb
5 3 4 1 7 6 2
output
Copy
3
input
Copy
bbbabb
bb
1 6 3 4 2 5
output
Copy
4
Note

In the first sample test sequence of removing made by Nastya looks like this:

"ababcba"  "ababba"  "abbba"  "abba"

Nastya can not continue, because it is impossible to get word "abb" from word "ababcba".

So, Nastya will remove only three letters.

 

题目大意:给两个字符串s1,s2和一个s1长度的数组od[ ],问按od[]给定的顺序去掉s1的字母,最多可以去掉多少个字母使s2仍然是s1的子串。

解题思路:本题暴力必定会超时,所以可以对数组进行二分查找。用一个Check函数检查s1去掉前mid位是否还能使s2是s1的子串,可以则head = mid + 1,否则tail = mid - 1。

代码:

 1 #include<cstdio>
 2 #include<cstdlib>
 3 #include<cmath>
 4 #include<cstring>
 5 #include<string>
 6 #include<iostream>
 7 #include<algorithm>
 8 #include<map>
 9 #include<stack>
10 #include<queue>
11 using namespace std;
12 typedef long long LL;
13 const LL MaxN = 2e5;
14 
15 string s1, s2;
16 int od[MaxN+5], len1, len2;
17 int head, tail, mid, cnt;
18 bool vis[MaxN + 5];
19 
20 int Check(int x)
21 {
22     memset(vis, 0, sizeof(vis));
23     cnt = 0;
24     for(int i = 0;i <= x;i++)
25     {
26         vis[od[i]-1] = 1;
27     }
28     for(int i = 0;i < len1;i++)
29     {
30         if(vis[i]) continue;
31         if(s1[i] == s2[cnt]) cnt++;
32         if(cnt == len2) return 1;
33     }
34     return 0;
35 }
36 
37 int main()
38 {
39     cin >> s1 >> s2;
40     len1 = s1.length();len2 = s2.length();
41     for(int i = 0;i < len1;i++)
42     {
43         scanf("%d", od + i);
44     }
45         head = 0, tail = len1-1;
46         while(head <= tail)
47         {
48             mid = (head + tail)/2;
49             if(Check(mid)) head = mid + 1;
50             else tail = mid - 1;
51             if(head > tail)
52             {
53                  if(tail == mid) printf("%d\n", mid+1);
54                  if(head == mid) printf("%d\n", mid);
55                  break;
56             }
57         }
58     return 0;
59 }

 

 

posted @ 2018-07-13 11:44  Lightfall  阅读(164)  评论(0编辑  收藏  举报