D. String Game
time limit per test

2 seconds

memory limit per test

512 megabytes

input

standard input

output

standard output

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.

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include<stack>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
char s1[200003],s2[200003];
int del[200003];
int vis[200003];
 
bool judge(int t, int s1_len, int s2_len)
{
    for(int i=0; i<s1_len; i++)
    {
        vis[i] = 0;
    }
    for(int i=0; i<t; i++)
    {
        vis[del[i] - 1] = 1;
    }
    int j = 0;
    for(int i=0; i<s1_len; i++)
    {
        if(vis[i]==0 && s1[i] == s2[j]) j++;
        if( j >= s2_len) return true;
    }
    return false;
}
 
int main()
{
    scanf("%s",s1);
    scanf("%s",s2);
    int length1 = strlen(s1);
    int length2 = strlen(s2);
    for(int i=0; i<length1; i++)
    {
        scanf("%d",&del[i]);
    }
    int l = 0;
    int r = length1 - 1;
    int ans;
    while(l <= r)
    {
        int mid = (l + r) >> 1;
        if(judge(mid, length1, length2)){
            l = mid + 1;
            ans = mid;
        }
        else{
            r = mid - 1;
        }
    }
    printf("%d",ans);
}

Codeforces Round #402 (Div. 2) D. String Game的更多相关文章

  1. Codeforces Round #402 (Div. 2) D. String Game(二分答案水题)

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

  2. Codeforces Round #402 (Div. 2) D String Game —— 二分法

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

  3. 【二分答案】Codeforces Round #402 (Div. 2) D. String Game

    二分要删除几个,然后暴力判定. #include<cstdio> #include<cstring> using namespace std; int a[200010],n, ...

  4. Codeforces Round #402 (Div. 2) A+B+C+D

    Codeforces Round #402 (Div. 2) A. Pupils Redistribution 模拟大法好.两个数列分别含有n个数x(1<=x<=5) .现在要求交换一些数 ...

  5. Codeforces Round #402 (Div. 2)

    Codeforces Round #402 (Div. 2) A. 日常沙比提 #include<iostream> #include<cstdio> #include< ...

  6. Codeforces Round #402 (Div. 2) A,B,C,D,E

    A. Pupils Redistribution time limit per test 1 second memory limit per test 256 megabytes input stan ...

  7. Codeforces Round #402 (Div. 2) A B C sort D二分 (水)

    A. Pupils Redistribution time limit per test 1 second memory limit per test 256 megabytes input stan ...

  8. Codeforces Round #402 (Div. 2) 题解

    Problem A: 题目大意: 给定两个数列\(a,b\),一次操作可以交换分别\(a,b\)数列中的任意一对数.求最少的交换次数使得任意一个数都在两个序列中出现相同的次数. (\(1 \leq a ...

  9. CodeForces Round #402 (Div.2) A-E

    2017.2.26 CF D2 402 这次状态还算能忍吧……一路不紧不慢切了前ABC(不紧不慢已经是在作死了),卡在D,然后跑去看E和F——卧槽怎么还有F,早知道前面做快点了…… F看了看,不会,弃 ...

随机推荐

  1. Redis使用和部分源码剖析以及Django缓存和redis的关系

    0.特点: a.持久化 b.单进程.单线程 c.5大数据类型    d.用于操作内存的软件.    e.虽然是缓存数据库但是可以做持久化的工作 MySQL是一个软件,帮助开发者对一台机器的硬盘进行操作 ...

  2. html总结:文本框填满表格

    <style> input { width: 100%; }</style>

  3. VO和DO转换(二) BeanUtils

    VO和DO转换(一) 工具汇总 VO和DO转换(二) BeanUtils VO和DO转换(三) Dozer VO和DO转换(四) MapStruct BeanUtils是Spring提供的,通常项目都 ...

  4. 我的集合学习笔记--ArrayList

    一,ArrayList 实现自己的ArrayList:主要是添加方法,理解自动扩容机制 代码+注释 package com.amazing.jdk.learn2List.list_08_13; /** ...

  5. winform使用相关

    1.回车键触发按钮点击事件——回车登录 设置窗体的AccessButton属性 2.密码框样式设置 设置PasswordChar为想要的密码显示的样式,如*  3.设置窗口居中 设置StartPosi ...

  6. Error Boundaries 错误边界

    错误边界是用于捕获其子组件树 JavaScript 异常,记录错误并展示一个回退的 UI 的 React 组件,而不是整个组件树的异常.错误边界在渲染期间.生命周期方法内.以及整个组件树构造函数内捕获 ...

  7. react-redux异步数据操作

    import React, { Component } from 'react'; import './App.css'; import {connect} from 'react-redux'; i ...

  8. JavaScript生成二维码图片

    1.引入一个二维码工具的js文件,同时需要引入jquery文件 下面是jquery.qrcode.min.js文件内容: (function(r){r.fn.qrcode=function(h){va ...

  9. Laravel 5.6 模型关联 user 表后查询 user 表数据只能获取第一条数据,不知道怎么获取第二条

    按照开发手册的说法,肯定是指令不够全,附代码图 如果, tests 是文章表, users 是用户表 test.com/tests/1 是 id 为 1 的文章地址( get 访问) 假如 Tests ...

  10. linux的使用

    第一 安装ubuntu操作系统 1. ubuntu下解决中英文输入法问题 问题: ubuntu在安装了搜狗输入法后无法切换英文,即使在搜狗输入法中设置了切换按键依然无反应, 原因在于当前系统中只有一个 ...