您的位置:首页 > 其它

CF-#402D String Game(二分+优先队列)

2017-03-06 20:35 465 查看
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"

1 #include <iostream>
2 #include <cstdio>
3 #include <cstring>
4 #include <algorithm>
5 #include <queue>
6 using namespace std;
7 priority_queue<int,vector<int>,greater<int> >que;
8 const int maxn=200005;
9 char p[maxn],t[maxn];
10 int a[maxn];
11 int n,len,len1;
12 int cheak(int mid)
13 {
14     while(!que.empty())
15         que.pop();
16     for(int i=mid; i<len; i++)
17         que.push(a[i]);
18     int flag=0,cnt=0;
19     while(!que.empty())
20     {
21         if(p[que.top()-1]==t[cnt])
22             cnt++;
23         que.pop();
24         if(cnt==len1)
25         {
26             flag=1;
27             break;
28         }
29     }
30     return flag;
31 }
32 int main()
33 {
34     while(~scanf("%s%s",p,t))
35     {
36         len=strlen(p);
37         len1=strlen(t);
38         for(int i=0; i<len; i++)
39             scanf("%d",&a[i]);
40         int l=0,r=len,ans=0;
41         while(l<=r)
42         {
43             int mid=(l+r)/2;
44             if(cheak(mid))
45             {
46                 l=mid+1;
47                 ans=mid;
48             }
49             else r=mid-1;
50         }
51         printf("%d\n",ans);
52     }
53     return 0;
54 }


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: