Codeforces Round #402 (Div. 2) D String Game —— 二分法
2 seconds
512 megabytes
standard input
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 t: a1... 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.
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).
Print a single integer number, the maximum number of letters that Nastya can remove.
ababcba
abb
5 3 4 1 7 6 2
3
bbbabb
bb
1 6 3 4 2 5
4
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 —— 二分法的更多相关文章
- 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 ...
- 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 ...
- 【二分答案】Codeforces Round #402 (Div. 2) D. String Game
二分要删除几个,然后暴力判定. #include<cstdio> #include<cstring> using namespace std; int a[200010],n, ...
- Codeforces Round #402 (Div. 2) A+B+C+D
Codeforces Round #402 (Div. 2) A. Pupils Redistribution 模拟大法好.两个数列分别含有n个数x(1<=x<=5) .现在要求交换一些数 ...
- Codeforces Round #402 (Div. 2)
Codeforces Round #402 (Div. 2) A. 日常沙比提 #include<iostream> #include<cstdio> #include< ...
- 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 ...
- 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 ...
- Codeforces Round #402 (Div. 2) 题解
Problem A: 题目大意: 给定两个数列\(a,b\),一次操作可以交换分别\(a,b\)数列中的任意一对数.求最少的交换次数使得任意一个数都在两个序列中出现相同的次数. (\(1 \leq a ...
- CodeForces Round #402 (Div.2) A-E
2017.2.26 CF D2 402 这次状态还算能忍吧……一路不紧不慢切了前ABC(不紧不慢已经是在作死了),卡在D,然后跑去看E和F——卧槽怎么还有F,早知道前面做快点了…… F看了看,不会,弃 ...
随机推荐
- [原创][FPGA]Quartus实用小技巧(长期更新)
0. 简介 在使用Quartus软件时,经常会时不时的发现一些小技巧,本文的目的是总结所查阅或者发现到的小技巧,本文长期更新. 1. Quartus中的模板功能 最近在Quartus II的菜单里找到 ...
- 快速掌握RabbitMQ(五)——搭建高可用的RabbitMQ集群
RabbitMQ的集群是依赖erlang集群的,而erlang集群是通过.erlang.cookie文件进行通信认证的,所以我们使用RabbitMQ集群时只需要配置一下.erlang.cookie文件 ...
- 搭建服务与负载均衡的客户端-Spring Cloud学习第二天(非原创)
文章大纲 一.Eureka中的核心概念二.Spring RestTemplate详解三.代码实战服务与负载均衡的客户端四.项目源码与参考资料下载五.参考文章 一.Eureka中的核心概念 1. 服务提 ...
- Maven创建Web工程并执行构建/测试/打包/部署
创建工程基本参考上一篇Java Application工程,不同的是命令参数变了,创建Web工程的命令如下: mvn archetype:generate -DgroupId=com.jsoft.te ...
- uitableview使用reloaddata不管用
原因在于决定row number得array变动后没有再次将其count赋值给numberOfRowsInSection中返回的成员变量.致使没有其作用
- Android 实现Activity后台运行
有时需要让activity在后台运行,具体实现方法如下: 在AndroidManifest.xml中,activity属性中增加: android:theme="@style/Backgro ...
- mac os+selenium2+Firefox驱动+python3
此文章建立在之前写的chrome+selenium+Python环境配置的基础上,链接http://blog.csdn.net/zxy987872674/article/details/5308289 ...
- svn hooks 实现自动更新
搞来搞去,原来是hooks 下面的脚本名称必须是post-commit才可以, 写成fly-commit一直不行.晕死~~~ https://serverfault.com/questions/144 ...
- js json 对象
JSON 语法规则 JSON 语法是 JavaScript 对象表示法语法的子集. 数据在名称/值对中 数据由逗号分隔 大括号保存对象 中括号保存数组 JSON 名称/值对 JSON 数据的书写格式是 ...
- iOS 插件制作
概述 我们平时也使用了非常多的xcode插件,尽管官方对于插件制作没有提供不论什么支持,可是载入三方的插件,默认还是被同意的.第三方的插件,须要存放在 ~/Library/Application Su ...