Codeforces Round #402 (Div. 2) D. String Game

本文介绍了一个字符串操作问题,即通过移除指定顺序的字符,使一个较长的字符串变为目标子串。文章提供了问题背景、输入输出格式及样例,并附带了解决该问题的C++代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


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 ta1... 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.


题意:给两个字符串p,t,再输入strlen(p)个数,只能依次按照这个顺序删除对应位置的字符,在p必须包含t的前提下p最多能删除多少个字符。

思路:直接二分答案判断是否满足条件即可,下面给代码:

#include<cstdio>  
#include<algorithm>  
#include<cstring>  
#include<iostream>  
#include<cmath>  
#include<queue>  
#include<functional>  
typedef long long LL;
using namespace std;
#define maxn 200005
#define ll l,mid,now<<1  
#define rr mid+1,r,now<<1|1  
#define lson l1,mid,l2,r2,now<<1  
#define rson mid+1,r1,l2,r2,now<<1|1  
#define inf 0x3f3f3f3f  
const int mod = 1e9 + 7;
char s1[maxn], s2[maxn];
int a[maxn], len1, len2, vis[maxn];
bool check(int nowlen){
	int pos1 = 0, pos2 = 0;
	memset(vis, 0, sizeof(vis));
	for (int i = 0; i < nowlen; i++)
		vis[a[i] - 1] = 1;
	while (pos1 < len1&&vis[pos1])
		pos1++;
	while (pos2 < len2&&pos1 < len1){
		if (s2[pos2] == s1[pos1++])
			pos2++;
		while (pos1 < len1&&vis[pos1])
			pos1++;
	}
	if (pos2 == len2)
		return true;
	return false;
}
int main(){
	scanf("%s%s", s1, s2);
	len1 = strlen(s1);
	len2 = strlen(s2);
	for (int i = 0; i < len1; i++){
		scanf("%d", &a[i]);
	}
	int l = 0, r = len1;
	int ans;
	while (l <= r){
		int mid = l + r >> 1;
		if (check(mid)){
			l = mid + 1;
			ans = mid;
		}
		else
			r = mid - 1;
	}
	printf("%d\n", ans);
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值