• 题意:给你一个模式串\(t\),现在要在主串\(s\)中删除多个子串,使得得到的\(s\)的子序列依然包含\(t\),问能删除的最长子串长度.

  • 题解:首先,我们不难想到,我们可以选择\(s\)头部到最右边的子序列的头部和最左边的子序列的尾部到\(s\)的尾部这两个子串,除去这两个子串,我们要找的最大子串一定在子序列的头部到尾部中,即子序列中两个相邻字符位置的间隔,那么很显然,我们想让相邻的字符之间相隔最大,所以问题也就转化成了:求模式串的相邻字符在主串中的最大间隔长度,最优的情况一定是最左的子序列到最右的子序列的间隔最大,我们可以正着反着记录模式串中每个字符第一次出现的位置,然后求差更新答案即可.

  • 代码:

    string s;
    string t;
    int pre[N];
    int suf[N]; int main() {
    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    cin>>s>>t;
    int lens=(int)s.size();
    int lent=(int)t.size(); int j=0;
    rep(i,0,lens){
    if(s[i]==t[j] && j<lent){
    pre[j++]=i;
    }
    } j=lent-1;
    per(i,lens-1,0){
    if(s[i]==t[j] && j>=0){
    suf[j--]=i;
    }
    } int ans=suf[0]; rep(i,0,lent-2){
    ans=max(ans,suf[i+1]-pre[i]-1);
    } ans=max(ans,lens-1-pre[lent-1]); cout<<ans<<'\n'; return 0;
    }

Codeforces Round #579 (Div. 3) D2. Remove the Substring (hard version) (思维,贪心)的更多相关文章

  1. Codeforces Round #540 (Div. 3) D2. Coffee and Coursework (Hard Version) (二分,贪心)

    题意:有\(n\)个数,每次可以选\(k(1\le k\le n)\)个数,并且得到\(a_1+max(0,a_2-1)+max(0,a_3-2)+...+max(0,a_k-k+1)\)的贡献,问最 ...

  2. CF #579 (Div. 3) D1.Remove the Substring (easy version)

    D1.Remove the Substring (easy version) time limit per test2 seconds memory limit per test256 megabyt ...

  3. Codeforces Round #540 (Div. 3) D1. Coffee and Coursework (Easy version) 【贪心】

    任意门:http://codeforces.com/contest/1118/problem/D1 D1. Coffee and Coursework (Easy version) time limi ...

  4. Codeforces Round #579 (Div. 3)

    Codeforces Round #579 (Div. 3) 传送门 A. Circle of Students 这题我是直接把正序.逆序的两种放在数组里面直接判断. Code #include &l ...

  5. 【cf比赛练习记录】Codeforces Round #579 (Div. 3)

    思考之后再看题解,是与别人灵魂之间的沟通与碰撞 A. Circle of Students 题意 给出n个数,问它们向左或者向右是否都能成一个环.比如样例5是从1开始向左绕了一圈 [3, 2, 1, ...

  6. Codeforces Round #579 (Div. 3) 题解

    比赛链接:https://codeforc.es/contest/1203/ A. Circle of Students 题意:\(T\)组询问,每组询问给出\(n\)个数字,问这\(n\)个数字能否 ...

  7. D2. Remove the Substring (hard version)(思维 )

    D2. Remove the Substring (hard version) time limit per test 2 seconds memory limit per test 256 mega ...

  8. D2. Remove the Substring (hard version)

    D2. Remove the Substring (hard version) 给字符串s,t,保证t为s的子序列,求s删掉最长多长的子串,满足t仍为s的子序列 记录t中每个字母在s中出现的最右的位置 ...

  9. Codeforces Round #579 (Div. 3) 套题 题解

    A. Circle of Students      题目:https://codeforces.com/contest/1203/problem/A 题意:一堆人坐成一个环,问能否按逆时针或者顺时针 ...

随机推荐

  1. 计算机科学: 寄存器&内存

    参考: [十分钟速成课:计算机科学]6.寄存器&内存 要想聊寄存器Latch,首先要聊内存.什么是内存? Memory,就是储存信息的东西. 我们都玩过单机游戏,如果突然关机,游戏结束但是没有 ...

  2. Docker 介绍和安装(一)

    # 下载阿里云的 Centos7 的docker.repo # step 1: 安装必要的一些系统工具 sudo yum install -y yum-utils device-mapper-pers ...

  3. Java 基于mail.jar 和 activation.jar 封装的邮件发送工具类

    准备工作 发送邮件需要获得协议和支持! 开启服务 POP3/SMTP 服务 如何开启 POP3/SMTP 服务:https://www.cnblogs.com/pojo/p/14276637.html ...

  4. three.js 之cannon.js物理引擎

    今天郭先生说的是一个物理引擎,它十分小巧并且操作简单,没错他就是cannon.js.这些优点都源自于他是基于js编写的,对于js使用者来说cannon.js拥有其他物理引擎没有的纯粹性.从学习成本来看 ...

  5. Received empty response from Zabbix Agent at [agent]. Assuming that agent dropped connection because of access permission

    Received empty response from Zabbix Agent at [agent]. Assuming that agent dropped connection because ...

  6. 【Linux】Linux下如何分区及如何格式化

    环境:CentOS7.1 磁盘大小是1.8T 将磁盘/dev/sda分一个分区,分区类型为xfs fdisk /dev/sda n    --创建新分区 p  --创建分区类型为主分区 1  --主分 ...

  7. Api文档自动生成工具

    java开发,根据代码自动生成api接口文档工具,支持RESTful风格,今天我们来学一下api-doc的生成 作者:互联网编程. 欢迎投稿,一起交流技术 https://www.jianshu.co ...

  8. SGA: allocation forcing component growth分析

    1.问题现象 20年12月31日,数据库应用人员反映2020-12-31 12:40:10存在告警,过了几分钟之后业务恢复正常. 表现的状态:Connect to database time out, ...

  9. AQS之ReentrantReadWriteLock写锁

    用法 1.1 定义一个安全的list集合 public class LockDemo { ArrayList<Integer> arrayList = new ArrayList<& ...

  10. kubernetes 核心技术-Controller 控制器

    一.什么是Controller? Controller是在集群上管理和运行容器的对象,Controller是实际存在的,Pod是抽象的,主要创建管理pod 二.Pod和Controller的关系 Po ...