Codeforces Round #402 (Div. 2) D. String Game 二分+优先队列+字符串匹配

来源:互联网 发布:shell数组遍历 编辑:程序博客网 时间:2024/05/06 11:48

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
ababcbaabb5 3 4 1 7 6 2
output
3
input
bbbabbbb1 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.



Source

Codeforces Round #402 (Div. 2)


My Solution

题意:给出文本串和目标串,然后给出一个文本串删除字符的序列,从a1~an,要求删除s[a1 ~ ak]时剩下的字符串依然可以匹配,求尽可能大的k。


二分+优先队列+字符串匹配

从左往右是删除字符的顺序,倒着做,从右往做是添加字符的顺序,故check mid~n这个序列能否满足目标串的匹配,要求mid尽可能小,

故二分这个剩余序列长度即可,每次check是用优先队列priority_queue<int, vector<int>, greater<int>> pq;,然后依次进行匹配。

复杂度 O(nlognlogn)


#include <iostream>#include <cstdio>#include <string>#include <vector>#include <queue>using namespace std;typedef long long LL;const int maxn = 2e5 + 8;string t, s;int v[maxn], n, szs;priority_queue<int, vector<int>, greater<int> > pq;inline bool check(const int &x){    int i, ptr = 0;    for(i = n; i > n - x; i--) pq.push(v[i]);    while(!pq.empty()){        if(s[ptr] == t[pq.top() - 1]) ptr++;        pq.pop();        if(ptr == szs) break;    }    while(!pq.empty()) pq.pop();    if(ptr == szs) return true;    else return false;}int main(){    #ifdef LOCAL    freopen("d.txt", "r", stdin);    //freopen("d.out", "w", stdout);    int T = 4;    while(T--){    #endif // LOCAL    ios::sync_with_stdio(false); cin.tie(0);    cin >> t >> s;    int i;    n = t.size(); szs = s.size();    for(i = 1; i <= n; i++){        cin >> v[i];    }    int l = 0, r = n, mid;    while(l + 1 < r){        mid = (l + r) >> 1;        if(check(mid)){            r = mid;        }        else l = mid;    }    if(check(l)) cout << n - l << endl;    else if(check(r)) cout << n - r << endl;    else cout << 0 << endl;    #ifdef LOCAL    cout << endl;    }    #endif // LOCAL    return 0;}


  Thank you!

                                                                                                                                             ------from ProLights

0 0
原创粉丝点击