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.

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include<stack>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
char s1[200003],s2[200003];
int del[200003];
int vis[200003];
 
bool judge(int t, int s1_len, int s2_len)
{
    for(int i=0; i<s1_len; i++)
    {
        vis[i] = 0;
    }
    for(int i=0; i<t; i++)
    {
        vis[del[i] - 1] = 1;
    }
    int j = 0;
    for(int i=0; i<s1_len; i++)
    {
        if(vis[i]==0 && s1[i] == s2[j]) j++;
        if( j >= s2_len) return true;
    }
    return false;
}
 
int main()
{
    scanf("%s",s1);
    scanf("%s",s2);
    int length1 = strlen(s1);
    int length2 = strlen(s2);
    for(int i=0; i<length1; i++)
    {
        scanf("%d",&del[i]);
    }
    int l = 0;
    int r = length1 - 1;
    int ans;
    while(l <= r)
    {
        int mid = (l + r) >> 1;
        if(judge(mid, length1, length2)){
            l = mid + 1;
            ans = mid;
        }
        else{
            r = mid - 1;
        }
    }
    printf("%d",ans);
}

最新文章

  1. Spring学习记录(十)---使用FactoryBean配置Bean
  2. LeetCode 118 Pascal&#39;s Triangle
  3. 樹的DFS和BFS
  4. Android使用ndk-stack获取so奔溃堆栈
  5. 异步编程:When.js快速上手
  6. Feature Scaling
  7. Cannot install ubuntu or other linux flavours on citrix Xen server
  8. ***RESTful API 设计指南(阮一峰)
  9. Zabbix实现告警分级
  10. [HIve - LanguageManual] LateralView
  11. Android网络框架Volley(实战篇)
  12. Java基础知识强化74:正则表达式之分割功能 (扩展练习)
  13. HDU2196-Computer
  14. LGPL与闭源程序
  15. C# 导出Word
  16. spring源码 — 四、MVC
  17. asp.net core系列 25 EF模型配置(隐藏属性)
  18. [是男人就过8题——Pony.ai]Perfect N-P Arrays
  19. JavaScript匿名函数入门。
  20. Shiro入门 - md5+salt测试

热门文章

  1. 06_Hadoop分布式文件系统HDFS架构讲解
  2. js处理ajax返回的json数组
  3. Navicat 或者Java的JDBC通过SSH Tunnel连接MySQL数据库
  4. CMD管道命令使用
  5. C#设计模式之1:策略模式
  6. HTTL之初印象
  7. 【学亮IT手记】使用Map代替switch...case语句
  8. jenkins了解一下,讲一下jenkins这个鬼东西
  9. oss上传和下载的笔记
  10. python之路-字符串