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看了看,不会,弃 ...
随机推荐
- BZOJ——2134: 单选错位
http://www.lydsy.com/JudgeOnline/problem.php?id=2134 Time Limit: 10 Sec Memory Limit: 259 MBSubmit: ...
- win10下ubuntu虚拟机互传文件
前言 用过虚拟机的都知道在向linux传文件的时候总是会遇到各种问题 安装虚拟机的增强工具不好用,反正就是各种麻烦各种麻烦 准备-------方案一 使用专门的xshell可以直接链接到虚拟机,同时配 ...
- iOS -- SKViedoNode类
SKViedoNode类 继承自 SKNode:UIResponder:NSObject 符合 NSCoding(SKNode)NSCopying(SKNode)NSObject(NSObject) ...
- 6.【nuxt起步】-完成一个静态的页面
1.接下来新建/component/maincontent.vue 把这些html代码copy到maincontent.vue 发现格式比较难看,就格式化一下 2.插件安装 beautify,安装后重 ...
- NFC模组,开发NFC功能 仅仅要几条指令的事情
特点:实现NFC透明传输.内置NFC协议栈,支持UART串口直接读写,用于门禁能够同一时候兼容手机和卡片开门,还能实现动态密钥,读到的NFC数据自己主动串口输出,会串口就能开发NFC,不须要研究LLC ...
- 关于C语言中二维数组传參————————【Badboy】
直接上代码: #include void Fun(int *a[],int m,int n)// { printf("%d\t",*a);//[0][0] /* int e[2][ ...
- PPAPI插件的动态创建、改动、删除
一旦你完毕了PPAPI插件的开发,实际使用时可能会有下列需求: 动态创建PPAPI插件 删除PPAPI插件 改变PPAPI插件的尺寸 实现起来非常easy,从JS里直接訪问DOM(BOM)就可以.以下 ...
- flexible.js + makegrid.js 自适应布局
一,flexible.js 的使用方式: (一),引用方式 1,引用cdn地址 <script src="http://g.tbcdn.cn/mtb/lib-flexible/0.3. ...
- construct-binary-tree-from-preorder-and-inorder-traversal——前序和中序求二叉树
Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume tha ...
- Codeforces 490F Treeland Tour 树上的最长上升子序列
题目链接:点击打开链接 题意: 给定n个点的树. 以下n个数表示点权. 以下n-1行给出树. 找一条链,然后找出这条链中的点权组成的最长上升子序列. 求:最长上升子序列的长度. 思路: 首先是维护一条 ...