D - String Game CodeForces - 779D
题目链接:https://vjudge.net/contest/160714#problem/D
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.
Example
ababcba
abb
5 3 4 1 7 6 2
3
bbbabb
bb
1 6 3 4 2 5
4
分析:使用2分查找,二分里加一个 ans=k 记录很实用
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
char t[200010],p[200010];
int a[200010];
int b[200010];
int l1,l2;
int bb(int k)
{
memset(b,0,sizeof(b));
for(int i=0;i<k;i++)
b[i]=a[i]-1;
sort(b,b+k);
int s=0;
int s1=0;
for(int j=0;t[j]!='\0';j++)
{
if(b[s1]==j)
s1++;
else if(p[s]==t[j])
s++;
}
if(s>=l2)
return 1;
return 0;
}
int main()
{
scanf("%s%s",t,p);
l1=strlen(t);
l2=strlen(p);
for(int i=0;i<l1;i++)
scanf("%d",&a[i]);
int l=0,r=l1-1;
int ans=0;
while(l<=r)
{
int k=(l+r)/2;
if(bb(k))
{
ans=k;
l=k+1;
}
else
r=k-1;
}
printf("%d",ans);
return 0;
}

浙公网安备 33010602011771号