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. Windows定时开机并开启工作软件

    开启休眠功能 在搜索窗口中输入“cmd.exe”,在结果中看见了“cmd.exe”,右击选择“以管理员权限运行程序”打开“cmd.exe”命令窗口,输入命令“powercfg -h on”即可开启计算 ...

  2. 学习Spring Security OAuth认证(一)-授权码模式

    一.环境 spring boot+spring security+idea+maven+mybatis 主要是spring security 二.依赖 <dependency> <g ...

  3. Hadoop介绍-3.HDFS介绍和YARN原理介绍

    一. HDFS介绍: Hadoop2介绍 HDFS概述 HDFS读写流程   1.  Hadoop2介绍 Hadoop是Apache软件基金会旗下的一个分布式系统基础架构.Hadoop2的框架最核心的 ...

  4. python中RabbitMQ的使用(交换机,广播形式)

    简介 如果要让每个接收端都能收到消息,此时需要将消息广播出去,需要使用交换机. 工作原理 消息发送端先将消息发送给交换机,交换机再将消息发送到绑定的消息队列,而后每个接收端都能从各自的消息队列里接收到 ...

  5. vue组件通信&&v兄弟组件通信eventbus遇到的问题(多次触发、第一次不触发)

    组件通讯包括:父子组件间的通信和兄弟组件间的通信.在组件化系统构建中,组件间通信必不可少的 (vuex以后再说). 父组件--> 子组件 1. 属性设置 父组件关键代码如下: <templ ...

  6. Jsop的原理

    Jsop的原理:利用script不存在跨域的问题,动态创建script标签,把需要请求的数据源地址赋值给其src属性,并且指定一个回调函数,从而接受到我们想要的数据

  7. 使用JQuery 合并两个 json 对象

    一,保存object1和2合并后产生新对象,若2中有与1相同的key,默认2将会覆盖1的值 var object = $.extend({}, object1, object2); 二,将2的值合并到 ...

  8. ISO/OSI七层网络参考模型、TCP/IP四层网络模型和教学五层网络模型

    一.说明 直接的原因是昨晚<计算机网络(自顶向下方法)>到货了,以为能讲得有些不一样,但看完整本也就是老调地讲过来讲应用层.传输层.网络层.网络接口层.感觉比之谢希仁的<计算机网络& ...

  9. springboot程序构建一个docker镜像(十一)

    准备工作 环境: linux环境或mac,不要用windows jdk 8 maven 3.0 docker 对docker一无所知的看docker教程. 创建一个springboot工程 引入web ...

  10. reload() 函数

    reload() 函数 当一个模块被导入到一个脚本,模块顶层部分的代码只会被执行一次. 因此,如果你想重新执行模块里顶层部分的代码,可以用 reload() 函数.该函数会重新导入之前导入过的模块.语 ...