D. String Game----二分答案
来源:互联网 发布:做淘宝客服要交钱吗 编辑:程序博客网 时间:2024/05/22 11:55
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.
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).
Print a single integer number, the maximum number of letters that Nastya can remove.
ababcbaabb5 3 4 1 7 6 2
3
bbbabbbb1 6 3 4 2 5
4
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.
16级省赛选拔防AK题,我发现cf好喜欢二分答案啊,我总感觉以前见过这个题,忘记了当时做没做出来,反正现在做出来了
题目的意思是说给你两个字符串,然后给你一串序列,然后我们按序列删除字母,删除后第一个串中还需要继续包括第二个串,求这个序列最长为多少。
匹配的时候我直接暴力的竟然没有超时,然后我们在序列中二分,然后就过了。。。好水
代码:
#include <cstdio>#include <cstring>#include <iostream>#include <string>using namespace std;string s;string ss;string S;int a[300000];bool judge(){ int len=ss.length(); int top=0; int L=S.length(); for(int i=0;i<L;i++){ if(ss[top]==S[i]){ top++; } } if(top==len) return true; return false;}int main(){ cin>>s; cin>>ss; int len=s.length(); for(int i=0;i<len;i++){ scanf("%d",&a[i]); } int ans=0; /*for(int i=0;i<len;i++){ s[a[i]-1]='*'; bool flag=judge(); if(!flag){ break; } ans++; }*/ int l=0,r=len-1; while(l<=r){ int mid=(l+r)>>1; S=s; for(int i=0;i<=mid;i++){ S[a[i]-1]='*'; } bool flag=judge(); if(!flag){ r=mid-1; } else{ l=mid+1; } } printf("%d\n",l); return 0;}
- D. String Game----二分答案
- CodeForces 779D String Game【二分答案】
- codeforces 799 D String Game (二分答案)
- Codeforces 779D String Game 二分
- codeforces 778D. String Game (二分搜索)
- 【Codeforces-402D】-String Game (二分)
- Codeforces 779D String Game 【二分】
- D. String Game 二分加字符串匹配
- binary string codeforces 862D 交互题,二分答案
- Codeforces Round #402 (Div. 2) D. String Game (二分)
- Codeforces Round #402 (Div. 2)D. String Game 二分
- Codeforces Round #402 (div. 2)D. String Game(二分)
- Codeforces Round #402 (Div. 2)D. String Game【二分】
- Codeforces Round #402 (Div. 2)D. String Game【二分】
- Codeforces Round #402 (Div. 2) D. String Game(二分)
- CF 779D String Game 二分,贪心匹配字符串
- Codeforces Round #402 (Div. 2) D.String Game 二分搜索
- (二分, 字符串)Codeforces Round #402 D. String Game
- oc——nonnull、nullable、null_resettable、__null_unspecified
- ios点击背景清除
- 函数参数
- C++学习笔记之——如何写一个异常处理
- js dropzone的使用方法
- D. String Game----二分答案
- 【EDA】EDA技术Quartus仿真步骤(图表仿真)
- Object android.content.Context.getSystemService(java.lang.String)' on a null object reference
- Velocity语法
- Android 通过注入js实现读取webview中图片并放大的优化
- bzoj2190 SDOI2008仪仗队
- 从一个文件中读取用户列表,来添加系统用户
- Burp Suite证书导入证书(https抓包)
- 二分查找----递归实现