Description
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|, allai are distinct).
Output
Print a single integer number, the maximum number of letters that Nastya can remove.
Examples
input
ababcbaabb5 3 4 1 7 6 2
output
3
input
bbbabbbb1 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.
题意:按照数字的顺序删除第一个字符串中的字符,问能不能得到子串是第二个字符串
解法:二分,反正是按照顺序来的,一个个尝试绝对不行,使用二分降低计算次数,然后记录需要删除的字符,对第二个字符进行比较,符合要求就减少范围,不符合范围就加大范围
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 #include <math.h> 5 #include <iostream> // C++头文件,C++完全兼容C 6 #include <algorithm> // C++头文件,C++完全兼容C 7 #define fre freopen("in.txt","r",stdin) //以文件代替控制台输入,比赛时很常用,能缩短输入测试样例的时间 8 #define INF 0x3f3f3f3f 9 #define inf 1e60 10 using namespace std; // C++头文件,C++完全兼容C 11 #define N 200005 // 宏定义 12 #define LL long long //宏定义 13 14 LL dp[N][2][15]; //第一个[]表示走到第几格,第二个[]表示第几行(0 或 1),第三个[]表示已经进行了几次换道 15 //其实第一个[]完全可以省略,因为进行操作的都是当前路段和前1个路段 16 int a[N],b[N]; 17 struct P 18 { 19 int x,y,ans; 20 }H[N]; 21 int ans; 22 bool cmd(P a,P b) 23 { 24 return a.ans<b.ans; 25 } 26 //记录每条道的值 27 int vis[N]; 28 int main() 29 { 30 string s1,s2; 31 cin>>s1>>s2; 32 for(int i=0;i<s1.size();i++) 33 { 34 cin>>a[i]; 35 } 36 int l=0,r=s1.size()-1; 37 while(l<=r) 38 { 39 ans=0; 40 int mid=(l+r)/2; 41 memset(vis,0,sizeof(vis)); 42 for(int i=0;i<=mid;i++) 43 { 44 vis[a[i]-1]=1; 45 } 46 for(int i=0;i<s1.size();i++) 47 { 48 if(vis[i]==0&&s2[ans]==s1[i]) 49 { 50 ans++; 51 } 52 } 53 if(ans==s2.size()) 54 { 55 l=mid+1; 56 } 57 else 58 { 59 r=mid-1; 60 } 61 } 62 cout<<l<<endl; 63 return 0; 64 }