778A String Game

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

 

 1 #include <iostream>
 2 #include <cstring>
 3 using namespace std;
 4 
 5 const int N = 2e5 + 10;
 6 char str1[N], str2[N];
 7 int len1, len2;
 8 int vis[N], num[N];
 9 
10 bool judge(int pos){//判断str1中是否含有str2
11     memset(vis,0,sizeof(vis));
12     for(int i = 0; i < pos; i++){
13         vis[num[i] - 1] = 1;// 标记被删除元素
14     }
15     int ans = 0;
16     for(int i = 0; i < len1; i++){
17         if(vis[i] == 0 && (str2[ans] == str1[i]))
18             ans++;
19         if(ans >= len2)//说明str1 中含有 str2
20             return true;
21     }
22     return false;
23 }
24 
25 int main(){
26     int ans;
27     cin >> str1 >> str2;
28     len1 = strlen(str1);
29     len2 = strlen(str2);
30     for(int i = 0; i < len1; i++){
31         cin >> num[i];
32     }
33     int mid;
34     int l = 0, r  = len1 - 1;
35     while(l <= r){//对 num[] 二分查找num[]中最后一个删除后符合的元素下标
36         mid = (l + r) >> 1;//即mid = (l + r) / 2;
37         if(judge(mid)){
38             l = mid + 1;
39             ans = mid;
40         }
41         else {
42             r = mid - 1;
43         }
44     }
45     cout << ans << endl;
46 }

 

转载于:https://www.cnblogs.com/jxust-jiege666/p/6641059.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值