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"
View Code
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
相关文章推荐
- cf gym101061J Cola(优先队列)
- CF_493 C- Vasya and Basketball(二分)
- cf 671/B Robin Hood(二分)@
- hiho 1269 优化延迟 二分 优先队列
- [二分] cf 768B Code For 1
- cf 320# A Problem about Polyline (二分)
- 2016 ACM/ICPC Asia Regional Qingdao Online hdu 5884 Sort (二分+优先队列)★
- CF 626C Block Towers 卿式二分
- LA 4254 Processor(二分+贪心+优先队列)
- 4538: [Hnoi2016]网络 链剖 + 堆(优先队列) / 整体二分
- CF 208E Blood Cousins(二分+DFS)
- 【打CF,学算法——四星级】CodeForces 689D Friends and Subsequences (RMQ+二分)
- Cf Edu 15 C 城市与信号塔[二分]
- cf 484B 二分+贪心
- uva 1422 - Processor(二分+优先队列)
- UVA 1422 Processor(优先队列+贪心+二分)
- CF Tavas and Karafs (二分)
- CF Spider's Web 二分
- cf 830A Office Keys 【二分+贪心】
- CF 567D(One-Dimensional Battle Ships-二分)