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 ...
随机推荐
- Spring Boot之默认连接池配置策略
注意:如果我们使用spring-boot-starter-jdbc 或 spring-boot-starter-data-jpa “starters”坐标,Spring Boot将自动配置Hikari ...
- android--------性能优化之Allocation Tracker
Allocation Tracker 能做什么? 追踪内存分配信息,按顺序排列,这样我们就能清晰看出来某一个操作的内存是如何一步一步分配出来的.比如在有内存抖动的可疑点,我们可以通过查看其内存分配轨迹 ...
- 6.4 操作契约 Operation Contracts
4.操作契约 Operation Contracts “用例描述”的补充 强调: 用例中重要的动作,其开始与结束是需要一些约束 4.5 操作契约的后置条件 定义Definition 后 ...
- Spring缓存注解
从3.1开始,Spring引入了对Cache的支持.其使用方法和原理都类似于Spring对事务管理的支持.Spring Cache是作用在方法上的,其核心思想是这样的:当我们在调用一个缓存方法时会把该 ...
- k8s问题收集
问题 解决 failed: rpc error: code = Unknown desc = NetworkPlugin cni failed to set up pod "*****&qu ...
- Oracle 三大范式
范式:数据库设计对数据的存储性能,还有开发人员对数据的操作都有莫大的关系.所以建立科学的,规范的的数据库是需要满足一些.规范的来优化数据数据存储方式.在关系型数据库中这些规范. 第一范式:数据库表中的 ...
- python-day96--git版本控制
1. 版本控制工具 - svn - git 2. git:软件帮助使用者进行版本的管理 3. git 相关命令 git init #初始化 初始化后,会 ...
- leetcode-algorithms-13 Roman to Integer
leetcode-algorithms-13 Roman to Integer Roman numerals are represented by seven different symbols: I ...
- 2015-09-21 css学习1
3.设置背景图片 Background-image:url(相对路径) ----123.jpg 图片拉伸铺满: background-size:cover 铺满方向: background-repea ...
- maven打包上传到本地中央库
pom文件中添加插件如下 <build> <plugins> <plugin> <groupId>org.apache.maven.plugins< ...