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.记录每个字符在第几次被删除,这样很巧妙。在判断时,只需知道这个字符的删除次序是否大于当前的删除次序,就可以判断它当前是否已经被删除。

如果已删除,则直接跳过,如果未删除,则判断字符是否相等,若相等,则j++。要是记录每次删除的字符哪个时,则必须要直接对字符串进行修改,

再用二分查找的话,则字符串将要改来改去,复杂。

2.用二分查找逐步逼近答案。

二分查找需要十分注意细节。

/*以后就规定二分查找这样用吧:

m = (h+t)/2;

h = m +1/-1;

t = m +1/-1;

不要用:

m = (h+t)/2;

h = m;

t = m;

因为这样可能会永远跳不出循环,例如当 h = 0;t  = 1,m = 0, 且h的条件符合,那么这3个数永远不会改变。

*/

学习之处:

1.用数组记录数据时,可以正向记录:

for(int i = ,t; i<n; i++)//这样的好处是顺着去记录,缺点是查找时必须遍历。这样记录重点在其存储的内容
{
scanf("%d",&t);
a[i] = t;
}

也可以反向记录:

for(int i = ,t; i<n; i++)//这样的好处是可以直接访问,缺点是元素分散,有哈希表的雏形。这样记录重点在于其功能。
{
scanf("%d",&t);
a[t] = i;
}

2.二分查找原来不只是查找数据,还能查找操作步骤啊,这就是举一反三?

3.似乎很多高明的做法都尽量不对原始数据进行改动,而是用另一种方法达到同样的效果。

代码如下:

 #include<bits/stdc++.h>  

 using namespace std;  

 char a[],b[];
int op[],lena,lenb; int ok(int last)//判断删除last个字符是否合法
{
int j = ;
for(int i = ; i<lena; i++)
{
if(op[i]>last)
{
if(a[i]==b[j]) j++;
if(j==lenb) return ;
}
}
return ;
} int main()
{
scanf("%s%s",a,b);
lena = strlen(a); lenb = strlen(b);
for(int i = ,j; i<=lena; i++)
{
scanf("%d",&j);
op[j-] = i;
} int h = ,t = lena, mid; /*这里需要十分注意:由于mid可能就是答案,但是由于mid合法,h仍然会继续加1,
所以结果可能会比实际答案多1,那么可以在h==t时,继续进行操作,这次操作其实
时判断结果是否合法,如果不合法,则t会-1,t成为正确答案。如果已经是正确答案,
那么h会+1,而t仍然为正确答案。所以两种情况中,t为正确答案。*/
while(h<=t)
{
mid = (h+t+)/;
if(ok(mid)) h = mid+;
else t = mid-;
}
printf("%d\n",t);
return ;
}

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. 洛谷——P1238 走迷宫

    P1238 走迷宫 题目描述 有一个m*n格的迷宫(表示有m行.n列),其中有可走的也有不可走的,如果用1表示可以走,0表示不可以走,文件读入这m*n个数据和起始点.结束点(起始点和结束点都是用两个数 ...

  2. Careercup | Chapter 4

    二叉查换树,左孩子小于等于根,右孩子大于根. 完全二叉树,除最后一层外,每一层上的节点数均达到最大值:在最后一层上只缺少右边的若干结点. complete binary tree 满二叉树,完美二叉树 ...

  3. [Violet 4] 毕业旅行

    2718: [Violet 4]毕业旅行 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 672  Solved: 389[Submit][Status ...

  4. Java调用Python程序

    最近,需要在应用中,需要使用Java程序去调用Python代码,主要有两种实现方式. 一.使用Jython架包 因为调用的Python代码中需要使用Json对象,开始使用jython2.5.2,发现不 ...

  5. Android在其他线程中更新UI

    public class TransferTools { private static final int MSG_START = 1001; private static final int MSG ...

  6. squirrelsql安装

    官网下载安装,第一次安装mac上,失败,后续重启mac看下.重启完后,还是起不来,估计和某些环境冲突,或者缺少环境 使用squirrelsql如何连接hive? http://lxw1234.com/ ...

  7. mac下报错 xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun

    如题mac下遇到错误: 解决办法:安装mac的命令行工具CommandLineTools xcode-select --install

  8. [LeedCode OJ]#85 Maximal Rectangle

     [ 声明:版权全部,转载请标明出处.请勿用于商业用途. 联系信箱:libin493073668@sina.com] 题目链接:https://leetcode.com/problems/maxima ...

  9. [Algorithm] JavaScript Graph Data Structure

    A graph is a data structure comprised of a set of nodes, also known as vertices, and a set of edges. ...

  10. poj 2528(区间改动+离散化)

    题意:有一个黑板上贴海报.给出每一个海报在黑板上的覆盖区间为l r,问最后多少个海报是可见的. 题解:由于l r取值到1e7,肯定是要离散化的,但普通的离散化会出问题.比方[1,10],[1,4],[ ...