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 ;
}

CodeForces - 779D String Game(二分)的更多相关文章

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

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

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

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

  3. Codeforces 799D. String Game 二分

    D. String Game time limit per test:2 seconds memory limit per test:512 megabytes input:standard inpu ...

  4. 779D. String Game 二分 水

    Link 题意: 给出两字符串$a$,$b$及一个序列,要求从前往后按照序列删掉$a$上的字符,问最少删多少使$b$串不为a的子串 思路: 限制低,直接二分答案,即二分序列位置,不断check即可. ...

  5. codeforces 779D - String Game

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

  6. [Codeforces 1199C]MP3(离散化+二分答案)

    [Codeforces 1199C]MP3(离散化+二分答案) 题面 给出一个长度为n的序列\(a_i\)和常数I,定义一次操作[l,r]可以把序列中<l的数全部变成l,>r的数全部变成r ...

  7. codeforces D. Mahmoud and Ehab and the binary string(二分)

    题目链接:http://codeforces.com/contest/862/submission/30696399 题解:这题一看操作数就知道是二分答案了.然后就是怎么个二分法,有两种思路第一种是找 ...

  8. Codeforces Round #402 D String Game(二分)

    [题目类型]二分答案 &题解: 只要你想到二分答案就不是难题了,但我当时确实是想不到. [时间复杂度]\(O(nlogn)\) &代码: #include <cstdio> ...

  9. 【codeforces 779D】String Game

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

随机推荐

  1. 认识Filter

    1). Filter 是什么 ? ①. JavaWEB 的一个重要组件, 可以对发送到 Servlet 的请求进行拦截, 并对响应也进行拦截. ②. Filter 是实现了 Filter 接口的 Ja ...

  2. hdu 4278 Faulty Odometer(进制转换)

    十进制转八进制的变形: #include<stdio.h> int main() { int n; while(scanf("%d",&n)!=EOF& ...

  3. win7桌面鼠标右键过慢的解决方案

    由于Intel显卡驱动在桌面右键的加载项造成.解决方案:清理注册表中桌面右键加载项,点击开始,输入regedit,找到HKEY_CLASSES_ROOT\Directory\Background\sh ...

  4. Aircrack使用

    Aircrack Aircrack-ng 组件功能之一就是采集WEP及WPA-PSK字典并应用无线端口扫描进行破解,具体组件说明如下: aircrack-ng 功能主要是WEP及WPA-PSK密码的恢 ...

  5. android获取USB设备的名称

    1.注释内 .是三星设备可能不支持,需要更换的代码. 2.mUsbManager.是getSystemService(Context.USB_SERVICE)获的. 3. 从stackoverflow ...

  6. Jquery Plugins Jquery Validate

      Jquery Validate 一.什么是Jquery Validate: jQuery Validate 插件为表单提供了强大的验证功能. 二.常用值: 1 required:true 必须输入 ...

  7. 【02】循序渐进学 docker:如何安装

    写在前面的话 我们接下来的操作都是 CentOS 7.5 以下完成的,为了避免你我结果不一致,建议你也采用 CentOS 7.5,原因如下: 1. 个人几年工作下来经历的公司,包括身边的运维朋友,90 ...

  8. openvpn的搭建与应用

    一.VPN概述: VPN(Virtual Private NetWork,虚拟专用网络)架设在公共共享的基础设施互联网上,在非信任的网络上建立私有的安全的连接,把分布在不同地域的办公场所.用户或者商业 ...

  9. Weekly Contest 117

    965. Univalued Binary Tree A binary tree is univalued if every node in the tree has the same value. ...

  10. 857. Minimum Cost to Hire K Workers

    There are N workers.  The i-th worker has a quality[i] and a minimum wage expectation wage[i]. Now w ...