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 ...
随机推荐
- Python+Appium自动化环境搭建
appium工作原理 appium 在android端工作流 client端也就是我们 test script是我们的webdriver测试脚本. 中间是起的Appium的服务,Appium在服务端起 ...
- Dubbo -- Simple Monitor
一.简介 dubbo-monitor-simple是dubbo提供的简单监控中心,可以用来显示接口暴露,注册情况,也可以看接口的调用明细,调用时间等. Simple Monitor挂掉不会影响到Con ...
- js 创建对象的几种方法
1. 使用object创建 var person = new Object(); person.name = "Tom"; person.age = "29"; ...
- 安卓GreenDao框架一些进阶用法整理(转)
大致分为以下几个方面: 一些查询指令整理 使用SQL语句进行特殊查询 检测表字段是否存在 数据库升级 数据库表字段赋初始值 一.查询指令整理 1.链式执行的指令 return mDaoSession. ...
- NumPy 统计函数
NumPy 统计函数 NumPy 提供了很多统计函数,用于从数组中查找最小元素,最大元素,百分位标准差和方差等. 函数说明如下: numpy.amin() 和 numpy.amax() numpy.a ...
- Linux之须知
1.服务器有哪些硬件? 主板,cpu,显卡,内存,硬盘,声卡,网卡 2.内存,CPU,硬盘的作用? cpu是管理与运算.内存:cpu和磁盘之间的缓冲设备,服务器关闭后,数据从内存中释放掉. CPU,电 ...
- 生成二维码的JAVA
不多说,上代码 package tcc; import java.awt.Color;import java.awt.Graphics2D;import java.awt.image.Buffered ...
- 使用phpStudyy运行tipask
tipask官网:https://www.tipask.com/tipask源码下载:https://www.tipask.com/download.html 可参考此处安装文档的链接 除此之外可以参 ...
- 15. 3Sum (重新分配数组大小)
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all un ...
- UmBasketella
UmBasketella http://poj.org/problem?id=3737 Time Limit: 1000MS Memory Limit: 65536K Total Submissi ...