Codeforces 778A(二分答案)

本文介绍Codeforces778A题目StringGame的解决思路,通过二分查找确定最多可删除字符数,确保剩余字符能构成目标字符串p。文章提供了一种使用优先队列进行字符匹配的有效方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Codeforces 778A String Game

题目:
String Game
time limit per test2 seconds
memory limit per test512 megabytes
inputstandard input
outputstandard output
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.

Input
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).

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

题意:
给定两个字符串t和p,以及a数组,按照a数组顺序删除字符串中字符,问最多可以删掉多少字符是剩下的字符串在只删去一些字符而不改变字符位置的情况下可以构成p字符串。

就是裸的二分答案,然而想了好久才想到2333,对总长度二分,然后判断区间内的字符能否组成p字符串,(我是借助一个优先队列,把字符串放在其中判断,使其按照原始顺序,不过似乎有好多种方法)

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <set>
#include <stack>
#include <set>
#include <queue>
#include <vector>
using namespace std;
char s[200005];
char p[200005];
char b[200005];
int a[200005];
struct xx
{
    char x;
    int y;
    bool operator < (xx m) const
    {
        return y>m.y;
    }
    xx(char m,int n){x=m,y=n;}
    xx(){}
};
priority_queue<struct xx> q;
bool check()
{
    int num=0,len=strlen(p);
    while(!q.empty()){
        struct xx x;
        x=q.top();
        q.pop();
        if(x.x==p[num]){
            num++;
        }
        if(num==len) return true;
    }
    return false;

}
int main()
{
    scanf("%s %s",s,p);
    int len=strlen(s);
    for(int i=0;i<len;i++){
        scanf("%d",&a[i]);
    }
    int l=0,r=len;
    while(l<r)
    {
        int mid=(l+r)/2;
        while(!q.empty()) q.pop();
        for(int i=mid;i<len;i++){
            q.push(xx(s[a[i]-1],a[i]));
        }
        if(check()){
            l=mid+1;

        }
        else r=mid;
    }
    printf("%d\n",r-1);
    return 0;
}
引用\[1\]:根据提供的引用内容,这段代码是一个用于查询第k小的修剪数字的函数。函数接受一个字符串数组nums和一个二维整数数组queries作为参数。函数首先将nums中的每个字符串截取为长度为m的子串,并将其与对应的索引存储在结构体数组a中。然后,对于每个查询,函数根据给定的修剪长度trim对a进行排序,并返回第ki个修剪数字的索引。最后,将所有查询的结果存储在一个整数数组ans中,并返回ans作为函数的结果。\[1\] 引用\[2\]:根据提供的引用内容,程序自动生成方法是一种机器根据用户需求自动生成相应代码的技术。智能化代码生成可以分为代码生成和代码补全两个任务。代码生成是根据开发人员使用自然语言编写代码的需求,机器生成特定编程语言的代码片段。代码补全是根据已编写代码上下文自动理解开发人员的编写意图并补全代码。根据补全的代码粒度,可以将其分为词元级别和行级别。代码生成任务的输入是自然语言描述,输出是能够实现自然语言描述功能的代码片段。本文研究的智能化代码生成限定于代码生成任务,即根据自然语言描述生成特定编程语言的代码片段。\[2\] 引用\[3\]:根据提供的引用内容,这段代码是一个Encrypter类的构造函数。构造函数接受三个参数:一个字符向量keys,一个字符串向量values和一个字符串向量dictionary。然而,在提供的引用中,构造函数的实现是空的,没有任何代码。因此,无法确定构造函数的具体功能和实现。\[3\] 问题:codeforces估分插件 回答:根据提供的引用内容,无法找到与codeforces估分插件相关的信息。请提供更多的上下文或引用内容,以便我能够为您提供更准确的答案。 #### 引用[.reference_title] - *1* *3* [leetcode cf各类比赛技巧](https://blog.csdn.net/qq_21237549/article/details/122993963)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [ChatGPT的代码生成是怎么做的?「基于深度学习的代码生成方法」最新研究进展...](https://blog.csdn.net/VucNdnrzk8iwX/article/details/129920030)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值