CF778A String Game 题解

news/2024/7/24 7:11:12 标签: c++, 算法, 二分

文章目录

  • CF778A String Game 题解
    • 题面翻译
    • Input Data
    • Output Data
    • Input Sample 1
    • Output Sample 1
    • 题目描述
    • 输入格式
    • 输出格式
    • 样例 #1
      • 样例输入 #1
      • 样例输出 #1
    • 样例 #2
      • 样例输入 #2
      • 样例输出 #2
    • 提示
    • 算法二分
    • 代码:

CF778A String Game 题解

link

题面翻译

给定两个由小写字母构成的字符串p和t,同时给定一个由数字 1 , 2 , 3... ∣ P ∣ 1,2,3...∣P∣ 1,2,3...P 组成的排列。(其中 ∣ p ∣ ∣p∣ p 表示字符串p的长度)按该排列顺序依次删除字符串 p p p 相应位置上的字母,删除过程中,约定各个字符的位置不变。请计算最多可以删除几次,字符串 p p p 中仍然包含字符串 t t t。(即字符串 t t t 仍然是字符串 p p p 的子序列)

数据保证有解

Input Data

第一行,一个字符串 p p p 1 ≤ ∣ p ∣ < ∣ t ∣ ≤ 200 , 0000 1≤∣p∣<∣t∣≤200,0000 1≤∣p∣<∣t∣≤200,0000

第二行,一个字符串 t t t

第三行,数字 1 1 1 ∣ p ∣ ∣p∣ p 组成的一个排列。

Output Data

一行,一个整数,表示最多删除的次数。

Input Sample 1

ababcba

abb

5 3 4 1 7 6 2

Output Sample 1

3

题目描述

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 t t and wants to get the word p p 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 t t : a 1 . . .   a ∣ t ∣ a_{1}...\ a_{|t|} a1... at . We denote the length of word x x x as ∣ x ∣ |x| x . Note that after removing one letter, the indices of other letters don’t change. For example, if t = t= t= “nastya” and a = [ 4 , 1 , 5 , 3 , 2 , 6 ] a=[4,1,5,3,2,6] 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 p 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 p p can be obtained by removing the letters from word t t t .

输入格式

The first and second lines of the input contain the words t t t and p p p , respectively. Words are composed of lowercase letters of the Latin alphabet ( $ <=|p|<|t|<=200000$ ). It is guaranteed that the word $ p $ can be obtained by removing the letters from word t t t .

Next line contains a permutation a 1 , a 2 , . . . , a ∣ t ∣ a_{1},a_{2},...,a_{|t|} a1,a2,...,at of letter indices that specifies the order in which Nastya removes letters of t t t ( 1 < = a i < = ∣ t ∣ 1<=a_{i}<=|t| 1<=ai<=t , all a i a_{i} ai 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.

算法二分

  1. 二分枚举什么?我们可以枚举删除的元素个数

  2. 可行性?假设删去 x x x 个元素可行,那么删去 x − 1 x - 1 x1 个元素也肯定可行。因此二分的序列有单调性,该二分成立。

  3. check 函数怎么写?判断删掉 x x x 个元素后是否包含序列 t t t 即可。

代码:

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

感谢大家的支持~


http://www.niftyadmin.cn/n/5382520.html

相关文章

【VSCode】设置 一键生成vue模板 的快捷入口

问题 每次写一个组件的时候&#xff0c;都需要去手敲默认结构或者是复制粘贴&#xff0c;十分的麻烦&#xff01; 解决办法 文件 > 首选项 > 用户代码片段 > vue.json 配置vue模板 其中prefix是用来触发代码段的内容&#xff0c;即模版的快捷入口&#xff1b;body里…

[AIGC] 字节跳动面试题:简单说说 JVM 的垃圾回收机制

在 Java 程序的运行过程中&#xff0c;会产生大量的对象&#xff0c;这些对象会占用内存空间。当对象不再被使用时&#xff0c;就需要释放它们所占用的内存&#xff0c;以避免内存泄漏和内存溢出等问题。这就是垃圾回收机制的作用。 在 JVM 中&#xff0c;垃圾回收机制主要包括…

kali虚拟机桥接模式快速设置

第一步&#xff1a;配置 IP、掩码、网关 vim /etc/network/interfaces第二步&#xff1a;配置 DNS&#xff1a; vi /etc/resolv.conf第三步&#xff1a;重启网卡 service networking restart如果还不行建议重启一下虚拟机

GitLab私有Git

GitLab私有Git 1 GitLab简介 GitLab是整个DevOps生命周期的第一个单一应用程序。只有GitLab才能启用Concurrent DevOps&#xff0c;从组件链的约束中解锁组织。GitLab提供无与伦比的可见性&#xff0c;更高的效率和全面的治理。这使得软件生命周期加快了200&#xff05;&…

SpringBoot+Kafka

文章目录 一、依赖二、配置文件三、API1、生产者2、消费者 一、依赖 <!-- spring-kafka&#xff08;与kafka的版本一致&#xff09; --> <dependency><groupId>org.springframework.kafka</groupId><artifactId>spring-kafka</artifactId>…

Web3区块链游戏:创造虚拟世界的全新体验

随着区块链技术的不断发展&#xff0c;Web3区块链游戏正逐渐崭露头角&#xff0c;为玩家带来了全新的虚拟世界体验。传统游戏中的中心化结构和封闭经济体系已经被打破&#xff0c;取而代之的是去中心化的游戏环境和真实所有权的数字资产。本文将深入探讨Web3区块链游戏的特点、…

领域驱动设计(Domain Driven Design)

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 文章目录 前言一、场景和要求二、领域模型关键词1.领域2.子域3.通用语言4.限界上下文5.领域模型6.实体和值对象7.聚合根8.领域服务9.领域事件 总结 前言 Domain Driven Desi…

虚拟机--pc端和macOS端互通

windows开启虚拟化 要在Windows系统中开启虚拟化&#xff0c;您可以按照以下步骤操作&#xff1a; 准备工作&#xff1a; 确保您的计算机CPU支持虚拟化技术。在BIOS中开启相应的虚拟化支持。 开启虚拟化&#xff1a; 打开控制面板&#xff0c;点击程序或功能项&am…