Codeforces Round #402 (Div. 2) D题 【字符串二分答案+暴力】

D. String Game

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.

Input

ababcba
abb

Output


Input

bbbabb
bb

Output


题意:给你一段操作序列;按顺序依次删掉字符串1中相应位置的字符;问你最多能按顺序删掉多少个字符;使得s2是剩下的字符构成的字符串的子列;

 思路:二分答案+暴力

AC代码:

 #include <bits/stdc++.h>
using namespace std; const int N = 2e5 + ;
char s1[N], s2[N];
bool vis[N];
int a[N], n, l2;
bool ok(){
for (int i = , j = ; i <= n && j <= l2; i++){
if (!vis[i]) continue;
if (s1[i] == s2[j]){
j++;
if (j > l2)
return true;
}
}
return false;
}
int main(){
scanf("%s", s1 + );
n = strlen(s1 + );
scanf("%s", s2 + );
l2 = strlen(s2 + );
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
int l = , r = n, ans = ;
while (l <= r){
int m = (l + r) >> ;
for(int i=;i<=n;i++)
vis[a[i]] = true;
for(int i=;i<=m;i++)
vis[a[i]] = false;
if (ok()){
ans = m;
l = m + ;
}
else
r = m - ;
}
printf("%d\n", ans);
return ;
}
上一篇:Linux之vi编辑器


下一篇:javascript第一弹——对象