codeforces链接:https://codeforces.com/problemset/problem/778/A

CF778A 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 $ : $ a_{1}...\ 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|<=200000 $ ). It is guaranteed that the word $ p $ can be obtained by removing the letters from word $ t $ .

Next line contains a permutation $ a_{1},a_{2},...,a_{|t|} $ of letter indices that specifies the order in which Nastya removes letters of $ t $ ( $ 1<=a_{i}<=|t| $ , all $ a_{i} $ are distinct).

输出格式

Print a single integer number, the maximum number of letters that Nastya can remove.

输入输出样例 #1

输入 #1

ababcba
abb
5 3 4 1 7 6 2

输出 #1

3

输入输出样例 #2

输入 #2

bbbabb
bb
1 6 3 4 2 5

输出 #2

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.

思路

这道题使用二分查找最后时间复杂度为O(n*log(n)),我们以最终删除的部分的位置为二分的部分

每次二分过程中进行一次check,如果该mid的索引下,符合要求,就更新答案,同时更新左端点为mid+1,思路比较简单,直接给出题解

题解

#include <bits/stdc++.h>
using namespace std;
const int N=2e5+10;
typedef long long ll;
string s,t;
int a[N];
int ans=0;
bool check(int x)
{
string s2 = s;
for(int i=0;i<x;i++)
{
s2[a[i]-1]=' ';
}
int ct=0;
for(int i=0;i<s.size();i++)
{
if(s2[i]==t[ct])ct++;
if(ct>t.size())break;
}
if(ct==t.size())return true;
else return false; }
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>s>>t;
for(int i=0;i<s.size();i++)cin>>a[i];
int l=0,r=s.size()-1;
while(l<=r)
{
int mid = (l+r)/2;
if(check(mid))
{
ans = mid;
l=mid+1;
}
else r=mid-1;
}
cout<<ans<<endl; return 0;
}

CF778A String Game (二分答案)的更多相关文章

  1. Codeforces Round #402 (Div. 2) D. String Game(二分答案水题)

    D. String Game time limit per test 2 seconds memory limit per test 512 megabytes input standard inpu ...

  2. {POJ}{3897}{Maze Stretching}{二分答案+BFS}

    题意:给定迷宫,可以更改高度比,问如何使最短路等于输入数据. 思路:由于是单调的,可以用二分答案,然后BFS验证.这里用优先队列,每次压入也要进行检查(dis大小)防止数据过多,A*也可以.好久不写图 ...

  3. CF 371C-Hamburgers[二分答案]

    C. Hamburgers time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  4. [NOIP2015]运输计划 D2 T3 LCA+二分答案+差分数组

    [NOIP2015]运输计划 D2 T3 Description 公元2044年,人类进入了宇宙纪元. L国有n个星球,还有n-1条双向航道,每条航道建立在两个星球之间,这n-1条航道连通了L国的所有 ...

  5. E. Santa Claus and Tangerines 二分答案 + 记忆化搜索

    http://codeforces.com/contest/752/problem/E 首先有一个东西就是,如果我要检测5,那么14我们认为它能产生2个5. 14 = 7 + 7.但是按照平均分的话, ...

  6. 字符串hash + 二分答案 - 求最长公共子串 --- poj 2774

    Long Long Message Problem's Link:http://poj.org/problem?id=2774 Mean: 求两个字符串的最长公共子串的长度. analyse: 前面在 ...

  7. codeforce 702C Cellular Network 二分答案

    http://codeforces.com/contest/702 题意:n个村庄,m个机站,问机站最短半径覆盖完所有村庄 思路:直接二分答案 二分太弱,调了半天..... // #pragma co ...

  8. 【二分答案+贪心】解决“最小值最大”问题(UVa 12124 - Assemble)

    Problem A - Assemble Time limit: 2 seconds Recently your team noticed that the computer you use to p ...

  9. POJ3294--Life Forms 后缀数组+二分答案 大于k个字符串的最长公共子串

                                                                              Life Forms Time Limit: 500 ...

  10. 【二分答案nlogn/标解O(n)】【UVA1121】Subsequence

    A sequence of N positive integers (10 < N < 100 000), each of them less than or equal 10000, a ...

随机推荐

  1. Data aggregation and group operations in pandas

    Data aggregation and group operations in pandas After loading,merging and preparing a dataset,you ma ...

  2. Tomcat安装与环境配置详解

    由于eclipse的很多开发版本不支持最新版的9.0,所以接下来下载的是8.0(钢铁直女的鼠标字丑的一批,极易引起不适,给各位猿兄打个预防针先) 官网地址:https://tomcat.apache. ...

  3. VSCode配置c++环境速通

    工作目录: (需要自己创建的文件) Build 文件夹下储存编译后的二进制文件 in 和 out 为文本文件,作为运行程序的输入和输出 所有 *.cpp 源码文件和 test.cpp 文件一样,放在工 ...

  4. 怒更一波TransDuck免费的声音克隆和AI配音功能

    宝子们! 最近咱软件TransDuck的免费声音克隆和AI配音功能被大家用爆啦!感谢各位自来水疯狂安利!! DD这里也是收到好多用户提的宝贵建议!所以,连夜肝了波更新! 这次重点更新使用克隆音色进行A ...

  5. http协议中长连接和短连接介绍

      TCP连接 当网络通信时采用TCP协议时,在真正的读写操作之前,客户端与服务器端之间必须建立一个连接,当读写操作完成后,双方不再需要这个连接时可以释放这个连接.连接的建立依靠"三次握手& ...

  6. golang遍历处理map时的常见性能陷阱

    最近一直在重构优化老系统,所以性能优化相关的文章会比较多. 这次的是有关循环处理map时的性能优化.预分配内存之类的大家都知道的就不多说了,今天来讲点大伙不知道的. 要讲的一共有三点,而且都和循环处理 ...

  7. java XML字符串和bean实体类互转

    pom引入依赖 <dependency> <groupId>com.fasterxml.jackson.dataformat</groupId> <artif ...

  8. NSMutableDictionary 的内存布局

    有关NSDictionary的内存布局,可以参看<NSDictionary 的内存布局>. 1 类图 和<NSDictionary 的内存布局>中的类图相比较,本章类图多了2个 ...

  9. 报表自动生成程序:ZREPORT_GENERATOR

    群里看到的,抄别人的...源自哪里就不清楚了.这里申明一下:非本人所写,如果侵权,你打群主好了. 这里说明一下:这个程序不太好用,没有SQVI友好.(以下问题可能是没用字段别名,不想试验了) 1.生成 ...

  10. 大模型VS小模型:论国产数据库运维AI Agent的正确打开方式

    作者:孙鹏,大衍(北京)科技有限公司研发工程师 首先为大家推荐这个 OceanBase 开源负责人老纪的公众号 "老纪的技术唠嗑局",会持续更新和 #数据库.#AI.#技术架构 相 ...