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.
题意:两个字符串t和p,再给定一个的整数序列a,依次删除字符串t中位置为ai的字符,求最多进行多少次删除操作,使剩下的字符串依然可以得到字符串p.
这题一个多小时没有一点思路,感觉怎么想都是超时,没想到二分序列a,平时也很少做二分的题。
思路:二分序列a 看删除其中的一些字符后是否能得到给定字符串,寻找满足条件的最大位置
code:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<vector>
#include<algorithm>
#include<iostream>
using namespace std;
const int N=2e5+10;
typedef long long LL;
int a[N],vis[N];
char s[N],t[N];
int main()
{
scanf("%s%s",s+1,t+1);
int len=strlen(s+1);
int len_t=strlen(t+1);
for(int i=1; i<=len; i++)
{
scanf("%d",&a[i]);
}
int l=1,r=len,mid,pos,ans=0;
while(l<=r)
{
mid=(l+r)/2;
memset(vis,0,sizeof(vis));
for(int i=1; i<=mid; i++)
{
vis[a[i]]++;
}
pos=1;
for(int i=1; i<=len; i++)
{
if(vis[i]==0)
{
if(s[i]==t[pos])
{
pos++;
}
}
if(pos>len_t)
break;
}
if(pos>len_t)
{
ans=mid;
l=mid+1;
}
else
r=mid-1;
}
printf("%d\n",ans);
}