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. 利用例子来理解spring的面向切面编程

    最近学习了spring的面向切面编程,在网上看到猴子偷桃的例子,觉得这种方式学习比书本上讲解有趣多了,也便于理解.现在就来基于猴子偷桃写个基本的例子. maven工程:

  2. 动态加载/删除css文件以及图片预加载

    动态加载/删除css文件以及图片预加载   功能模块页面   最近,工作中遇到了一个比较奇葩的需求:要在一个页面(PC端)增加一个功能模块,但是这个页面在不久之后要重构,为了新增加的模块可以继续复用, ...

  3. SilverLight:基础控件使用(1)

    ylbtech-SilverLight-Basic-Control:基础控件使用(1) 本文详解控件有: Label, TextBox, PasswordBox, Image, Button , Ra ...

  4. JS里面的call, apply以及bind

    参考了这篇文章:http://www.tuicool.com/articles/EVF3Eb 给几个例子 function add(a,b) { alert(a+b); } function sub( ...

  5. Android开源工具项目集合

    最近因为要去外派了,工欲善其事,必先利其器!所以又回顾了一下自己github上所收藏的项目,也算是温故而知新吧. 最流行的Android组件大全 http://www.open-open.com/li ...

  6. POJ3592 Instantaneous Transference 强连通+最长路

    题目链接: id=3592">poj3592 题意: 给出一幅n X m的二维地图,每一个格子可能是矿区,障碍,或者传送点 用不同的字符表示: 有一辆矿车从地图的左上角(0,0)出发, ...

  7. Mondiran创建连接

    曾经使用jdbc创建连接的时候使用的url是这种形式:jdbc:mysql://hostname:port/database?key1=value1&key2=value2,在URL须要以&q ...

  8. navicat小经验和快捷键

    1.有时按快捷键Ctrl+F搜某条数据的时候搜不到,但是能用sql查出来,这是怎么回事? Ctrl+F只能搜本页数据,不在本页的数据搜不到,navicat每页只显示1000条数据.在数据多的时候nav ...

  9. SolidEdge 工程图中如何给零件添加纹理或贴图

    格式-检视-勾选纹理   选中一个零件之后,点击格式-面,在纹理选项卡中找到纹理的贴图   最后效果如下图所示,如果不勾选检视纹理,则虽然的确贴图了,但是不显示出来给你看.如果贴图文件没了,也不会显示 ...

  10. JSP技术基础(动态网页基础)

    前言:如果说html为静态网页基础,那么jsp就是动态网页基础,两者的区别就是jsp在html的前面多加了几行而已.当然,jsp里面对java的支持度更高.要明白,js只是嵌入在客户端的小程序小脚本而 ...