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肯定包含序列b,然后给了一个删除序列,删除给定位置的字符,然后依照顺序删,看最多能删几个

思路:一开始肯定想的是直接遍历,然后用个标记数组直接记录哪个被删了,然后就T了

范围是 (1 ≤ |p| < |t| ≤ 200 000).然后我们想下,我们就是想求一个看能最多删几个的东西,去遍历可以得到,但是一般遍历能得到的东西,我们就可以想到二分,

如果删这么多满足的话,就继续往后二分,如果不行往前二分,时间复杂度就可以满足了

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>
#include <queue>
#include <string>
#include <set>
#include <map>
using namespace std;
const int maxn = 1e6+;
char a[maxn];
char b[maxn];
char tmp[maxn];
int c[maxn],n;
bool slove(int pos)
{
strcpy(tmp,a);
for(int i=;i<pos;i++)
tmp[c[i]-] = ;
int res = ;
for(int i=;i<n;i++)
{
if(tmp[i]==b[res])
res++;
if(res==strlen(b))
return true;
}
if(res==strlen(b))
return true;
else
return false;
}
int main()
{
scanf("%s %s",a,b);
n = strlen(a);
for(int i=;i<n;i++)
scanf("%d",&c[i]);
int l = ,r = n;
int k = ;
while(k--)
{
int mid = (l+r)/;
if(slove(mid))
l = mid;
else
r = mid;
}
printf("%d\n",l);
return ;
}

要熟练掌握二分思想和满足二分的情况

刷多点题提高敏感度

CodeForces - 779D的更多相关文章

  1. 【codeforces 779D】String Game

    [题目链接]:http://codeforces.com/contest/779/problem/D [题意] 给你一段操作序列; 按顺序依次删掉字符串1中相应位置的字符; 问你最多能按顺序删掉多少个 ...

  2. CodeForces 779D. String Game(二分答案)

    题目链接:http://codeforces.com/problemset/problem/779/D 题意:有两个字符串一个初始串一个目标串,有t次机会删除初始串的字符问最多操作几次后刚好凑不成目标 ...

  3. codeforces 779D - String Game

    time limit per test 2 seconds memory limit per test 512 megabytes input standard input output standa ...

  4. CodeForces - 779D String Game 常规二分

    题意:给你两个串,S2是S1 的一个子串(可以不连续).给你一个s1字符下标的一个排列,按照这个数列删数,问你最多删到第几个时S2仍是S1 的一个子串. 题解:二分删掉的数.判定函数很好写和单调性也可 ...

  5. CodeForces - 779D String Game(二分)

    Little Nastya has a hobby, she likes to remove some letters from word, to obtain another word. But i ...

  6. Codeforces #402

    目录 Codeforces #402 Codeforces #402 Codeforces 779A Pupils Redistribution 链接:http://codeforces.com/co ...

  7. python爬虫学习(5) —— 扒一下codeforces题面

    上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...

  8. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  9. 【Codeforces 738C】Road to Cinema

    http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...

随机推荐

  1. 11月24日 layouts and rendering in rails(部分没有看)

    http://guides.rubyonrails.org/layouts_and_rendering.html  中文 This guide covers the basic layout feat ...

  2. 发布web站点数据库环境的问题

    今天在公司部署web站点的时候发现了一个比较低级的错误,公司映射的外网的IP我们内部人员是无法进行访问的,结果我把站点配置文件中的IP改成是外网的IP,站点一直无法访问到数据.后面发现问题是,当外网用 ...

  3. centOS 6.5安装python和nginx

    一.安装python3.5 1.安装python3.5

  4. string用scanf读入printf输出(节省时间)

    #include <iostream> #include <stdio.h> #include <string.h> using namespace std; in ...

  5. Python基础之文件的基本操作

    概述:文件的基本操作1.open 打开文件 f = open("xxx",mode="r",encoding="utf-8") #常用形式 ...

  6. 欧拉函数 牛客寒假1 小a与黄金街道

    题目链接 分析:这题用到了欧拉函数, 欧拉函数,用φ(n)表示 欧拉函数是求小于等于n的数中与n互质的数的数目 详细可以看看这篇博文https://www.cnblogs.com/linyujun/p ...

  7. 『MXNet』im2rec脚本使用以及数据读取

    一.im2rec用法简介 首先看文档: usage: im2rec.py [-h] [--list] [--exts EXTS [EXTS ...]] [--chunks CHUNKS] [--tra ...

  8. 3月22 关于CSS

    CSS(Cascading Style Sheep 叠层样式表,作用是美化HTML网页)/*注释内容*/ 为注释的方法. 样式表的分类: 1.内联样式表 和HTML联合显示,控制精确,但是可重用性差, ...

  9. Matlab:非线性热传导(抛物方程)问题

    函数文件1:real_fun.m function f=real_fun(x0,t0) %精确解 f=4*x0*(1-x0)*sin(t0); 函数文件2:F.m function f=F(N,u,U ...

  10. python-flask-script定制manage命令

    安装: pip3 install flask-script #!/usr/bin/env python # -*- coding:utf-8 -*- from flask_script import ...