码迷,mamicode.com
首页 > 其他好文 > 详细

Codeforces Round #402 D(二分)

时间:2017-02-27 20:39:44      阅读:543      评论:0      收藏:0      [点我收藏+]

标签:ice   examples   oal   before   this   rem   sel   orm   term   

                                                                                     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.

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.

a b两个字符串 

我们只需要对答案而分   判断a是否包含b

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<queue>
#include<map>
#include<set>
#include<vector>
#include<cstdlib>
#include<string>
#define eps 0.000000001
typedef long long ll;
typedef unsigned long long LL;
using namespace std;
const int N=200000+100;
char str[N],b[N];
int a[N];
int vis[N];
int len,len1;
int judge(int x){
    for(int i=0;i<len;i++)vis[i]=0;
    for(int i=0;i<x;i++)vis[a[i]-1]=1;
    int ans=0;
    for(int i=0;i<len;i++){
        if(vis[i]==0&&str[i]==b[ans])ans++;
        if(ans>=len1)return 1;
    }
    return 0;
}
int main(){
    while(cin>>str){
        cin>>b;
        len=strlen(str);
        len1=strlen(b);
        for(int i=0;i<len;i++)cin>>a[i];
        int l=0;
        int r=len-1;
        int mid;
        int ans;
        while(l<=r){
            mid=(l+r)>>1;
            if(judge(mid)){
                l=mid+1;
                ans=mid;
            }
            else
                r=mid-1;
        }
        cout<<ans<<endl;
    }
}

 

Codeforces Round #402 D(二分)

标签:ice   examples   oal   before   this   rem   sel   orm   term   

原文地址:http://www.cnblogs.com/Aa1039510121/p/6475938.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!