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. HDU 4349 Xiao Ming's Hope 找规律

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4349 Xiao Ming's Hope Time Limit: 2000/1000 MS (Java/ ...

  2. 扩展欧几里得算法(exGCD)学习笔记

    @(学习笔记)[扩展欧几里得] 本以为自己学过一次的知识不会那么容易忘记, 但事实证明, 两个星期后的我就已经不会做扩展欧几里得了...所以还是写一下学习笔记吧 问题概述 求解: \[ax + by ...

  3. javascript 函数初探 (六)--- 闭包初探#2

    首先,我们需要声明一个全局函数的占位符.尽管这种占位符不是必须的,但最好还是声明一下,然后我们重新将函数F()定义一下: var inner; var F = fucntion(){ var b = ...

  4. Myeclipse 出现了Could not generate DH keypair 错误

    在myeclipse下安装svn插件,出现了Could not generate DH keypair,这么一个错误. 这个问题困扰了我半天时间,各种百度也找不到答案,或许是百度能力问题吧.百度出来的 ...

  5. mysql delete auto_increment列时的注意问题

    1. 说明 在对带有AUTO_INCREMENT列的表delete掉所有数据时: (1)对于MyISAM表,在delete表中所有数据时没有任何风险,随意折腾: (2)对于InnoDB表,在delet ...

  6. struts1.3中使用DispatchAction的一个问题

    近期做项目发现我们公司的项目是用struts1写的,在多方百度下,总有理解了struts1.3的DispatchAction的使用方法 一:struts.xml文件的配置 <?xml versi ...

  7. mysql创建还原点

    set autocommit = 0;   insert into t1(name) values ("user1"); savepoint p1;   insert into t ...

  8. C语言函数的递归和调用

    函数记住两点: (1)每个函数运行完才会返回调用它的函数:每个函数运行完才会返回调用它的函数,因此,你可以先看看这个函数不自我调用的条件,也就是fun()中if条件不成立的时候,对吧,不成立的时候就是 ...

  9. svn 命令个

    svn 命令行下常用的几个命令 标签: svnpathdelete工作urlfile 2011-11-28 08:16 128627人阅读 评论(1) 收藏 举报  分类: 版本控制(8)  版权声明 ...

  10. 如何与强势的人相处zz

    要和强势的人相处良好,须知道强势的人有两个很显著的特点:一.以自我观点为中心.二.怕别人否定自己.强势的主要作用也有两个:一.支配别人.二.掩盖自卑. 首先,要区分一下强势的人和特立独行的人,这两类人 ...