778A 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.
#include <iostream>
#include <cstring>
using namespace std; const int N = 2e5 + ;
char str1[N], str2[N];
int len1, len2;
int vis[N], num[N]; bool judge(int pos){//判断str1中是否含有str2
memset(vis,,sizeof(vis));
for(int i = ; i < pos; i++){
vis[num[i] - ] = ;// 标记被删除元素
}
int ans = ;
for(int i = ; i < len1; i++){
if(vis[i] == && (str2[ans] == str1[i]))
ans++;
if(ans >= len2)//说明str1 中含有 str2
return true;
}
return false;
} int main(){
int ans;
cin >> str1 >> str2;
len1 = strlen(str1);
len2 = strlen(str2);
for(int i = ; i < len1; i++){
cin >> num[i];
}
int mid;
int l = , r = len1 - ;
while(l <= r){//对 num[] 二分查找num[]中最后一个删除后符合的元素下标
mid = (l + r) >> ;//即mid = (l + r) / 2;
if(judge(mid)){
l = mid + ;
ans = mid;
}
else {
r = mid - ;
}
}
cout << ans << endl;
}
778A String Game的更多相关文章
- Codeforces 778A:String Game(二分暴力)
http://codeforces.com/problemset/problem/778/A 题意:给出字符串s和字符串p,还有n个位置,每一个位置代表删除s串中的第i个字符,问最多可以删除多少个字符 ...
- 透过WinDBG的视角看String
摘要 : 最近在博客园里面看到有人在讨论 C# String的一些特性. 大部分情况下是从CODING的角度来讨论String. 本人觉得非常好奇, 在运行时态, String是如何与这些特性联系上的 ...
- JavaScript String对象
本编主要介绍String 字符串对象. 目录 1. 介绍:阐述 String 对象的说明以及定义方式. 2. 实例属性:介绍 String 对象的实例属性: length. 3. 实例方法:介绍 St ...
- ElasticSearch 5学习(9)——映射和分析(string类型废弃)
在ElasticSearch中,存入文档的内容类似于传统数据每个字段一样,都会有一个指定的属性,为了能够把日期字段处理成日期,把数字字段处理成数字,把字符串字段处理成字符串值,Elasticsearc ...
- [C#] string 与 String,大 S 与小 S 之间没有什么不可言说的秘密
string 与 String,大 S 与小 S 之间没有什么不可言说的秘密 目录 小写 string 与大写 String 声明与初始化 string string 的不可变性 正则 string ...
- js报错: Uncaught RangeError: Invalid string length
在ajax请求后得到的json数据,遍历的时候chrome控制台报这个错误:Uncaught RangeError: Invalid string length,在stackoverflow查找答案时 ...
- c# 字符串连接使用“+”和string.format格式化两种方式
参考文章:http://www.liangshunet.com/ca/201303/218815742.htm 字符串之间的连接常用的两种是:“+”连接.string.format格式化连接.Stri ...
- 【手记】注意BinaryWriter写string的小坑——会在string前加上长度前缀length-prefixed
之前以为BinaryWriter写string会严格按构造时指定的编码(不指定则是无BOM的UTF8)写入string的二进制,如下面的代码: //将字符串"a"写入流,再拿到流的 ...
- JavaScript中String对象的方法介绍
1.字符方法 1.1 charAt() 方法,返回字符串中指定位置的字符. var question = "Do you like JavaScript?"; alert(ques ...
随机推荐
- iOS Dev (25) 解决“The executable was signed with invalid entitlements.”问题
2014-01-10 10:34 5240人阅读 评论(1) 收藏 举报 目录(?)[+] iOS Dev (25) 解决“The executable was signed with inv ...
- ABAP 常用函数
函数名 描述 SD_VBAP_READ_WITH_VBELN 根据销售订单读取表vbap中的信息EDIT_LINES 把READ_TEXT返回的LINES中的行按照TDFORMAT=“*”重新组织VI ...
- Linux安装face_recgnition
Ubuntu 3:apt-get install python3.6-dev 4:pip3 install face_recgnition 5: pip3 install opencv-python ...
- 《深入理解JAVA虚拟机》----------第三章 垃圾收集器与内存分配策略,笔记(下)
1.垃圾收集器 1.1 Serial收集器 这个收集器是一个单线程的收集器,它在进行垃圾收集时,必须暂停其他所有的工作线程. 它是虚拟机运行在Client模式下的默认新生代收集器,它简单而高效. 1. ...
- mui-当使用addeleventlisener()方法绑定事件时选择器无法绑定事件
在mui中绑定事件不能用jQuery或mui(“#XX”)的形式选取某个元素,而是document.getelementbyid()的形式 mui(“#XX”)可以使用on方法绑定事件
- git flow分支管理
阅读目录 两种核心分支 三种临时分支 Git Flow流程示例代码 Git Flow工具 分支命名规范 总结 git flow是Vincent Driessen提出了一个分支管理的策略,非常值得借鉴. ...
- Bootstrap 导航元素(标签页)
[Bootstrap 导航元素] 1.基本的导航元素:标签导航.基于ul.li而来,给ul添加 class="nav nav-tabs" 即可.选中的li添加 class=&quo ...
- EF CodeFirst学习笔记001--主键约定
Code First 的核心是约定,这些默认的规则使我们可以用我们自己的类来创建模型.EF框架要求一个类必须有一个键属性.规则约定如果一个属性名为Id或者是类名+Id的形式(如PatientId),这 ...
- telnet ip/域名 端口 是否成功
有时候会ping ip 通,但是telnet不通,可能端口未开. telnet不成功,则显示不能打开到主机的链接,链接失败 . telnet成功,则进入telnet页面(全黑的),证明端口可用.
- awk技巧 nginx access.log
1.1 介绍 awk其名称得自于它的创始人 Alfred Aho .Peter Weinberger 和 Brian Kernighan 姓氏的首个字母.实际上 AWK 的确拥有自己的语言: AWK ...