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. RPC实战与核心原理之动态分组

    动态分组:超高效实现秒级扩缩容 回顾 在 RPC 里面怎么支持流量回放,应用在引入 RPC 后,所有的请求都会被 RPC 接管,而我们在 RPC 里面引入回放的原因也很简单,就是想通过线上流量来验证改 ...

  2. Merge Two Binary Trees——LeetCode进阶路

    原题链接https://leetcode.com/problems/merge-two-binary-trees/ 题目描述 Given two binary trees and imagine th ...

  3. SgLang代码细读-2.forward过程

    SgLang代码细读-2.forward过程 总览 Forward的主要过程围绕着 run_batch->TPModelWorker->ModelRunner->Model-> ...

  4. 基于FPGA的超声波雷达感应预警系统 全过程记录

    FPGA系统开发 综合实验记录 实验时间节点与想法记录 2023.4.24 新建本文档.目前决定有以下两个方案,要根据学校发的器件和自己的水平和后面时间决定. 课设想法 具体情况 基于FPGA的高速运 ...

  5. Spring Boot2.5 集成数据库连接池 HikariCP

    目录 §工程环境 §数据库连接池介绍 数据库连接池工作原理剖析 §Java常见数据库连接池性能比较 §数据库连接池选型 Druid vs HikariCP性能对比 §HikariCP为什么这么快 §数 ...

  6. Go交叉编译

    #在Mac上编译linux平台应用 GOOS=linux GOARCH=amd64 go build hello.go #在Windows上编译linux平台应用(关闭CGO) CGO_ENABLED ...

  7. java等比压缩图片工具类

    工具类 package com.chinaums.abp.util; import javax.imageio.ImageIO; import java.awt.*; import java.awt. ...

  8. Springboot笔记<14>指标监控

    指标监控 未来每一个微服务在运算部署以后,我们都需要对其进行监控.追踪.审计和控制等等.Springboot就抽取了Actuator场景,使得我们每个微服务快速引用即可获得生产级别的应用监控.审计等功 ...

  9. PI Errors and possible solutions

    引自:https://wiki.scn.sap.com/wiki/display/ERPHCM/PI+Errors+and+possible+solutions 转至元数据起始   404 - Not ...

  10. stm32达到什么程度叫精通?

    作为一个在嵌入式领域摸爬滚打了快10年的老兵,看到这个问题时我陷入了深深的思考.精通?这两个字说起来轻松,但要真正做到却是另一回事.我记得刚入行的时候,觉得会用几个库函数就算"精通" ...