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。

代码:

 #include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<string>
#include<iostream>
#include<algorithm>
#include<map>
#include<stack>
#include<queue>
using namespace std;
typedef long long LL;
const LL MaxN = 2e5; string s1, s2;
int od[MaxN+], len1, len2;
int head, tail, mid, cnt;
bool vis[MaxN + ]; int Check(int x)
{
memset(vis, , sizeof(vis));
cnt = ;
for(int i = ;i <= x;i++)
{
vis[od[i]-] = ;
}
for(int i = ;i < len1;i++)
{
if(vis[i]) continue;
if(s1[i] == s2[cnt]) cnt++;
if(cnt == len2) return ;
}
return ;
} int main()
{
cin >> s1 >> s2;
len1 = s1.length();len2 = s2.length();
for(int i = ;i < len1;i++)
{
scanf("%d", od + i);
}
head = , tail = len1-;
while(head <= tail)
{
mid = (head + tail)/;
if(Check(mid)) head = mid + ;
else tail = mid - ;
if(head > tail)
{
if(tail == mid) printf("%d\n", mid+);
if(head == mid) printf("%d\n", mid);
break;
}
}
return ;
}

最新文章

  1. JQuery 实现标题与内容相呼应样式
  2. 【MVVMLight小记】二.开发一个简单图表生成程序附源码
  3. 清北学堂模拟day6 花
  4. Framework7--Test
  5. JSP--监听HTTP会话
  6. [转]vector iterator not incrementable 的问题
  7. html5 拖曳功能的实现[转]
  8. 【Nhibernate】入门 踩雷篇
  9. ASCII码表详解
  10. JS常用扩展
  11. Windows 注册表操作
  12. HDOJ 5017 Ellipsoid
  13. A.Alyona and copybooks
  14. Docker之常用命令(二)
  15. Mysql远程访问报错2003
  16. Java中级开发工程师知识点归纳
  17. Appium-处理系统弹窗
  18. Jquery中addClass方法不起作用的解决方案
  19. struts2中的constant介绍之struts.objectFactory与spring的整合
  20. JAVA8-让代码更优雅之List排序

热门文章

  1. solrCloud选举leader的逻辑分析
  2. [luogu3369]普通平衡树(替罪羊树模板)
  3. 448. Find All Numbers Disappeared in an Array 寻找有界数组[1,n]中的缺失数
  4. 面试题:volatile关键字的作用、原理
  5. 基于PCL绘制模型并渲染
  6. ROS naviagtion analysis: costmap_2d--Costmap2DROS
  7. Hyperledger Fabric1.0 整体结构
  8. laravel实现多对多的分析
  9. Ubuntu 14.04 安装配置强大的星际译王(stardict)词典
  10. (BST)升序数组变为BST树