1. 程式人生 > >Codeforces 778A(二分答案)

Codeforces 778A(二分答案)

Codeforces 778A String Game

題目:
String Game
time limit per test2 seconds
memory limit per test512 megabytes
inputstandard input
outputstandard 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 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.

題意:
給定兩個字串t和p,以及a陣列,按照a陣列順序刪除字串中字元,問最多可以刪掉多少字元是剩下的字串在只刪去一些字元而不改變字元位置的情況下可以構成p字串。

就是裸的二分答案,然而想了好久才想到2333,對總長度二分,然後判斷區間內的字元能否組成p字串,(我是藉助一個優先佇列,把字串放在其中判斷,使其按照原始順序,不過似乎有好多種方法)

AC程式碼:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <set>
#include <stack>
#include <set>
#include <queue>
#include <vector>
using namespace std;
char s[200005];
char p[200005];
char b[200005];
int a[200005];
struct xx
{
    char x;
    int y;
    bool operator < (xx m) const
    {
        return y>m.y;
    }
    xx(char m,int n){x=m,y=n;}
    xx(){}
};
priority_queue<struct xx> q;
bool check()
{
    int num=0,len=strlen(p);
    while(!q.empty()){
        struct xx x;
        x=q.top();
        q.pop();
        if(x.x==p[num]){
            num++;
        }
        if(num==len) return true;
    }
    return false;

}
int main()
{
    scanf("%s %s",s,p);
    int len=strlen(s);
    for(int i=0;i<len;i++){
        scanf("%d",&a[i]);
    }
    int l=0,r=len;
    while(l<r)
    {
        int mid=(l+r)/2;
        while(!q.empty()) q.pop();
        for(int i=mid;i<len;i++){
            q.push(xx(s[a[i]-1],a[i]));
        }
        if(check()){
            l=mid+1;

        }
        else r=mid;
    }
    printf("%d\n",r-1);
    return 0;
}