Codeforces 799D. String Game 二分
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.
题目链接:http://codeforces.com/contest/779/problem/D
题意:有2个字符串a,b。将a按照顺序删除,问a最多删除多少个字母后,依然包含b。保证数据有效,即一定有结果。
思路:二分。将a要删除的个数进行二分,每次二分后暴力寻找。时间复杂度为n*16.注意二分的姿势。
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const int MAXN=2e5+;
char a[MAXN],b[MAXN];
int t[MAXN];
int main()
{
scanf("%s%s",a,b);
int n=strlen(a),m=strlen(b);
for(int i=; i<=n; i++)
{
int x;
scanf("%d",&x);
t[x-]=i;
}
int l=,r=n;
int ans=;
while(l<=r)
{
int mid=(l+r)/;
int i=,j=;
int flag=;
for(i=; i<n; i++)
{
if(t[i]<=mid) continue;
if(a[i]==b[j]) j++;
if(j==m) flag=;
}
if(flag==) l=mid+,ans=mid;
else r=mid-;
}
cout<<ans<<endl;
return ;
}
二分
Codeforces 799D. String Game 二分的更多相关文章
- [Codeforces 1199C]MP3(离散化+二分答案)
[Codeforces 1199C]MP3(离散化+二分答案) 题面 给出一个长度为n的序列\(a_i\)和常数I,定义一次操作[l,r]可以把序列中<l的数全部变成l,>r的数全部变成r ...
- codeforces D. Mahmoud and Ehab and the binary string(二分)
题目链接:http://codeforces.com/contest/862/submission/30696399 题解:这题一看操作数就知道是二分答案了.然后就是怎么个二分法,有两种思路第一种是找 ...
- CodeForces 779D. String Game(二分答案)
题目链接:http://codeforces.com/problemset/problem/779/D 题意:有两个字符串一个初始串一个目标串,有t次机会删除初始串的字符问最多操作几次后刚好凑不成目标 ...
- Codeforces Round #402 D String Game(二分)
[题目类型]二分答案 &题解: 只要你想到二分答案就不是难题了,但我当时确实是想不到. [时间复杂度]\(O(nlogn)\) &代码: #include <cstdio> ...
- CodeForces - 779D String Game 常规二分
题意:给你两个串,S2是S1 的一个子串(可以不连续).给你一个s1字符下标的一个排列,按照这个数列删数,问你最多删到第几个时S2仍是S1 的一个子串. 题解:二分删掉的数.判定函数很好写和单调性也可 ...
- CodeForces - 779D String Game(二分)
Little Nastya has a hobby, she likes to remove some letters from word, to obtain another word. But i ...
- Codeforces 862D. Mahmoud and Ehab and the binary string 【二分】(交互)
<题目链接> 题目大意: 有一个长度为n(n<1000)的01串,该串中至少有一个0和一个1,现在由你构造出一些01串,进行询问,然后系统会给出你构造的串与原串的 Hamming ...
- Codeforces.862D.Mahmoud and Ehab and the binary string(交互 二分)
题目链接 \(Description\) 有一个长为\(n\)的二进制串,保证\(01\)都存在.你可以询问不超过\(15\)次,每次询问你给出一个长为\(n\)的二进制串,交互库会返回你的串和目标串 ...
- codeforces 803D Magazine Ad(二分+贪心)
Magazine Ad 题目链接:http://codeforces.com/contest/803/problem/D ——每天在线,欢迎留言谈论. 题目大意: 给你一个数字k,和一行字符 例: g ...
随机推荐
- 使用yii\filters下的比如\PageCache需要在web.php里面的组件上配置'cache' => [ 'class' => 'yii\caching\FileCache', ],
public function behaviors(){ /*需要在config文件下的web.php里面加上 'cache' => [ 'class' => 'yii\caching\F ...
- 内存占用过高 kill 调整mysql内存占用
通过 /var/log/messages 查看 被系统kill掉的进程 如果是自己崩溃会产生 hs_err_ 修改mysql my.cnf innodb_buffer_pool_siz ...
- echarts 树图
1 事件:事件绑定,事件命名统一挂载到require('echarts/config').EVENT(非模块化为echarts.config.EVENT)命名空间下,建议使用此命名空间作为事件名引用, ...
- Android Studio 统计行数
开发中常常会想看看自己累积在这个项目中写了多少代码了,以下就是在Android Studio查看统计项目代码总行数的方法. 打开Android Studio,按快捷键Ctrl+Shift+A 输入fi ...
- 2.2、CDH 搭建Hadoop在安装(安装Java Development Kit)
第2步:安装Java Development Kit 要安装Oracle JDK,您可以使用Cloudera Manager安装Cloudera提供的版本,也可以直接安装Oracle的其他版本. 继续 ...
- cakePHP的ajax弹出窗
在html里添加一个触发弹出框的按钮 $("#button1").on("click", function() { $("#dialogue" ...
- linux按键映射
.Xmodmap keycode = Mode_switch keysym k = k K Left keysym j = j J Down keysym l = l L Right keysym h ...
- elasticsearch ik安装
/usr/share/elasticsearch/bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-ana ...
- Maven 添加jar包到本地仓库
一.使用Maven命令安装jar包 前提:在windows操作系统中配置好了Maven的环境变量,怎么配置请自己百度,这里不介绍,可参考https://jingyan.baidu.com/articl ...
- CentOS Netstat命令
语法 netstat(选项) 选项 -a或--all:显示所有连线中的Socket: -A<网络类型>或--<网络类型>:列出该网络类型连线中的相关地址: -c或--conti ...