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. ubuntu 下正确安装android手机驱动

    1. 查看手机ID号. charlesxue@THSHIBA:~/setup/cocos2d-x/cocos2d-x-/projects/simpleGame/proj.android/bin$ ls ...

  2. 665. Non-decreasing Array只允许修改一位数的非递减数组

    [抄题]: Given an array with n integers, your task is to check if it could become non-decreasing by mod ...

  3. web 应用中访问 Spring 具体实现

    user=LF password=LF jdbcUrl=jdbc:oracle:thin:@localhost:1521:orcl driverClass=oracle.jdbc.driver.Ora ...

  4. SqlServer—大话函数依赖与范式

    说明:数据库中的某些概念真的很让人头疼,概念的东西本来就是很枯燥的,再加上枯燥的学习,那就更加枯燥了.概念这东西,你不理解也能生产东西,经验多了就行,但是为了更深入的学习,你还必须理解.这里,我抛开书 ...

  5. Inception安装

    前言: MySQL语句需要审核,这一点每个DBA及开发人员都懂,但介于语句及环境的复杂性,大部分人都是望而却步,对其都是采取妥协的态度,从而每个公司都有自己的方法. 大多数公司基本都是半自动化(脚本+ ...

  6. Solidity开发、测试、部署

    这篇文章很详细的列举了几种方式来开始solidity开发: https://medium.com/@davekaj/solidity-tips-and-tricks-for-beginners-bui ...

  7. rpmbuild SPEC语法

    rpmbuild SPEC语法 摘自:http://bbs.chinaunix.net/thread-4179207-1-1.html spec文件写作规范 2008-09-28 11:52:17 分 ...

  8. 用JQuery获取输入框中的光标位置

    (function ($, undefined) { $.fn.getCursorPosition = function () { var el = $(this).get(0); var pos = ...

  9. python,itertools模块

    itertools模块的使用: # coding=utf-8 """ itertools模块 """ import itertools im ...

  10. App测试从入门到精通之性能测试

    好了,上节我们介绍了关于APP测试的功能测试方面一些细节.这一篇我们来介绍一下,关于APP测试过程中的性能测试参考要点,我们需要思考的如下: 响应时间 1.APP安装卸载的响应时间 2.APP各种功能 ...