codeforces 779d
来源:互联网 发布:参知政事和同平章事 编辑:程序博客网 时间:2024/05/18 06:24
D. String Game
time limit per test
2 seconds
memory limit per test
512 megabytes
input
standard input
output
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.
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
input
ababcba
abb
5 3 4 1 7 6 2
output
3
input
bbbabb
bb
1 6 3 4 2 5
output
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.
这道题是看别人的代码才会的;
利用二分的思想,总共能删除的数目是strlen(a)-strlen(b)这么多个,所以对于x[]的前strlen(a)-strlen(b)这么多个进行二分;
详见下代码:
#include<cstdio>#include<cstring>#include<algorithm>using namespace std; int x[200005];char a[200005],b[200005],c[200005];int ok(int th){ for(int i=0;i<th;i++) a[x[i]-1]='#';/**/ int j=0; for(int i=0;a[i]!='\0';i++) { if(a[i]==b[j]) j++; if(b[j]=='\0') return 1; } return 0;} int main(){ scanf("%s",a); scanf("%s",b); strcpy(c,a); int lena,lenb,lenc; lena=strlen(a),lenb=strlen(b),lenc=strlen(c); for(int i=0;i<lena;i++) scanf("%d",&x[i]); int l=0,r=lena-lenb,ans=0; while(l<=r) { int mid=(l+r)/2; if(ok(mid)) l=mid+1,ans=mid; else r=mid-1;/**/ strcpy(a,c); } printf("%d\n",ans); return 0;}
- codeforces 779D 二分
- 【CodeForces】779D
- codeforces 779D
- codeforces 779d
- 【codeforces 779D】String Game
- codeforces 779 D. String Game
- codeforces contest 779 D题
- Codeforces 779D String Game 二分
- Codeforces 779D String Game 【二分】
- CodeForces 779D String Game【二分答案】
- CodeForces 617D CodeForces 617D
- CodeForces 101D
- CodeForces 103D
- CodeForces 222D Olympiad
- codeforces 242d
- CodeForces 111D
- 【codeforces】3D
- CodeForces 127D Password
- IntelliJ Idea编译报错:javacTask: 源发行版 1.7 需要目标发行版 1.7
- 监听EditText输入的字数
- 所有CSS伪类/元素
- 地图构建算法分析整理
- git 通过 https.proxy 下载github上的项目
- codeforces 779d
- Oracle JOB 案例
- 【DayDayUp】【算法_图_欧拉回路_之一_Fleury (弗罗莱) 算法】
- Centos下Yum安装PHP5.5,5.6,7.0
- Django shell
- Android Studio(Gradle) 解决库依赖 冲突问题
- 筋斗云接口编程 / 日志与调试
- cqoi2013 新Nim游戏
- 我是一个线程 【转】