CodeForces - 779D
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.
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
ababcba
abb
5 3 4 1 7 6 2
3
bbbabb
bb
1 6 3 4 2 5
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.
题意:当初看这个题看了快一小时了还是不理解,我。。。。
就是给了两个序列,序列a肯定包含序列b,然后给了一个删除序列,删除给定位置的字符,然后依照顺序删,看最多能删几个
思路:一开始肯定想的是直接遍历,然后用个标记数组直接记录哪个被删了,然后就T了
范围是 (1 ≤ |p| < |t| ≤ 200 000).然后我们想下,我们就是想求一个看能最多删几个的东西,去遍历可以得到,但是一般遍历能得到的东西,我们就可以想到二分,
如果删这么多满足的话,就继续往后二分,如果不行往前二分,时间复杂度就可以满足了
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>
#include <queue>
#include <string>
#include <set>
#include <map>
using namespace std;
const int maxn = 1e6+;
char a[maxn];
char b[maxn];
char tmp[maxn];
int c[maxn],n;
bool slove(int pos)
{
strcpy(tmp,a);
for(int i=;i<pos;i++)
tmp[c[i]-] = ;
int res = ;
for(int i=;i<n;i++)
{
if(tmp[i]==b[res])
res++;
if(res==strlen(b))
return true;
}
if(res==strlen(b))
return true;
else
return false;
}
int main()
{
scanf("%s %s",a,b);
n = strlen(a);
for(int i=;i<n;i++)
scanf("%d",&c[i]);
int l = ,r = n;
int k = ;
while(k--)
{
int mid = (l+r)/;
if(slove(mid))
l = mid;
else
r = mid;
}
printf("%d\n",l);
return ;
}
要熟练掌握二分思想和满足二分的情况
刷多点题提高敏感度
CodeForces - 779D的更多相关文章
- 【codeforces 779D】String Game
[题目链接]:http://codeforces.com/contest/779/problem/D [题意] 给你一段操作序列; 按顺序依次删掉字符串1中相应位置的字符; 问你最多能按顺序删掉多少个 ...
- CodeForces 779D. String Game(二分答案)
题目链接:http://codeforces.com/problemset/problem/779/D 题意:有两个字符串一个初始串一个目标串,有t次机会删除初始串的字符问最多操作几次后刚好凑不成目标 ...
- codeforces 779D - String Game
time limit per test 2 seconds memory limit per test 512 megabytes input standard input output standa ...
- 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 #402
目录 Codeforces #402 Codeforces #402 Codeforces 779A Pupils Redistribution 链接:http://codeforces.com/co ...
- python爬虫学习(5) —— 扒一下codeforces题面
上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- 【Codeforces 738C】Road to Cinema
http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...
随机推荐
- CF-822C Hacker, pack your bags! 思维题
题目大意是给若干线段及其费用,每个线段权值即为其长度.要求找出两个不重合线段,令其权值和等于x且费用最少. 解法: 先分析一下题目,要处理不重合的问题,有重合的线段不能组合,其次这是一个选二问题,当枚 ...
- day11 - 15(装饰器、生成器、迭代器、内置函数、推导式)
day11:装饰器(装饰器形成.装饰器作用.@语法糖.原则.固定模式) 装饰器形成:最简单的.有返回值的.有一个参数的.万能参数 函数起的作用:装饰器用于在已经完成的函数前后增加功能 语法糖:使代码变 ...
- 2018-2019 ACM-ICPC Brazil Subregional Programming Contest
A:留坑 B:二维sg函数,特判边界情况 //#pragma GCC optimize(2) //#pragma GCC optimize(3) //#pragma GCC optimize(4) / ...
- Git:合并分支冲突问题
首先创建分支feature1,并且修改readme.txt内容,再在该分支上提交. 切换回master分支, 并且修改readme.txt内容,再在master分支上提交. 此时的分支路线如下,合并时 ...
- html中传递信息
<div class="card" data-username="ArgenBarbie"> </div> JS: var userna ...
- SecureCRT修改显示行数
Scrollback buffer应该是保留的行数,初始值500,修改成自己想要的数值保存即可. 参考:http://blog.csdn.net/w410589502/article/details/ ...
- shiro jwt 构建无状态分布式鉴权体系
一:JWT 1.令牌构造 JWT(json web token)是可在网络上传输的用于声明某种主张的令牌(token),以JSON 对象为载体的轻量级开放标准(RFC 7519). 一个JWT令牌的定 ...
- Java 求两个数百分比%
int num1 = 500; int num2 = 312; // 创建一个数值格式化对象 NumberFormat numberFormat = NumberFormat.getInstance( ...
- linux 添加环境变量(php为例)
find / -name php vim /etc/profile 文件最后添加 export PATH=$PATH:/usr/local/php/bin source /etc/profile p ...
- git Please move or remove them before you can merge.
git clean -d -fx "" 其中 x -----删除忽略文件已经对git来说不识别的文件 d -----删除未被添加到git的路径中的文件 f -----强制运行