CF778A String Game (二分答案)

codeforces链接:https://codeforces.com/problemset/problem/778/A

CF778A 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 $ : $ a_{1}...\ 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|<=200000 $ ). It is guaranteed that the word $ p $ can be obtained by removing the letters from word $ t $ .

Next line contains a permutation $ a_{1},a_{2},...,a_{|t|} $ of letter indices that specifies the order in which Nastya removes letters of $ t $ ( $ 1<=a_{i}<=|t| $ , all $ a_{i} $ are distinct).

输出格式

Print a single integer number, the maximum number of letters that Nastya can remove.

输入输出样例 #1

输入 #1

ababcba
abb
5 3 4 1 7 6 2

输出 #1

3

输入输出样例 #2

输入 #2

bbbabb
bb
1 6 3 4 2 5

输出 #2

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.

思路

这道题使用二分查找最后时间复杂度为O(n*log(n)),我们以最终删除的部分的位置为二分的部分
每次二分过程中进行一次check,如果该mid的索引下,符合要求,就更新答案,同时更新左端点为mid+1,思路比较简单,直接给出题解

题解

#include <bits/stdc++.h>
using namespace std;
const int N=2e5+10;
typedef long long ll;
string s,t;
int a[N];
int ans=0;
bool check(int x)
{
    string s2 = s;
    for(int i=0;i<x;i++)
    {
        s2[a[i]-1]=' ';
    }
    int ct=0;
    for(int i=0;i<s.size();i++)
    {
        if(s2[i]==t[ct])ct++;
        if(ct>t.size())break;
    }
    if(ct==t.size())return true;
    else return false;

}
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin>>s>>t;
    for(int i=0;i<s.size();i++)cin>>a[i];
    int l=0,r=s.size()-1;
    while(l<=r)
    {
        int mid = (l+r)/2;
        if(check(mid))
        {
            ans = mid;
            l=mid+1;
        }
        else r=mid-1;
    }
    cout<<ans<<endl;
    
    return 0;
}
posted @ 2025-05-26 21:12  屈臣  阅读(29)  评论(0)    收藏  举报