D2. Remove the Substring (hard version)

思路:其实就是贪心吧,先从前往后找,找到 t 可在 s 中存在的最小位置 (pre),再从后往前找,找到 t 可在 s 中存在的最大位置(last),然后 last [ i+1 ] - pre [ i ] - 1 表示的即是 t 中两个相邻字符可以删去的最大连续字串长度,当然也不要忘记第一个字符前面删去( last [ 0 ] )的和最后末尾删去的字符串(slen - pre [ tlen - 1 ] - 1)

代码:

// Created by CAD on 2019/8/14.
#include <bits/stdc++.h>
using namespace std;
using ll=long long; string s,t;
const int maxn=2e5+5;
int pre[maxn],last[maxn];
int main()
{
cin>>s>>t;
int flag=0,slen=s.length(),tlen=t.length();
for(int i=0;flag<tlen;++i)
if(s[i]==t[flag])
pre[flag++]=i;
flag=tlen-1;
for(int i=slen-1;flag>=0;i--)
if(s[i]==t[flag])
last[flag--]=i;
int ans=max(last[0],slen-pre[tlen-1]-1);
for(int i=0;i+1<tlen;++i)
ans=max(ans,last[i+1]-pre[i]-1);
cout<<ans<<endl;
}

Remove the Substring的更多相关文章

  1. 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 ...

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

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

  3. D2. Remove the Substring (hard version)

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

  4. 双指针(最大删除子串)Codeforces Round #579 (Div. 3)--Remove the Substring (hard version)

    题目链接:https://codeforces.com/contest/1203/problem/D2 题意: 给你S串.T串,问你最长删除多长的子串使得S串里仍然有T的子序列. 思路: 想了好久,先 ...

  5. Codeforces - 1203D2 - Remove the Substring (hard version) - 双指针

    https://codeforces.com/contest/1203/problem/D2 上次学了双指针求两个字符串之间的是否t是s的子序列.但其实这个双指针可以求出的是s的前i个位置中匹配t的最 ...

  6. CF1203D2 Remove the Substring (hard version) 题解

    这题初赛让我白给了6分,于是我决定回来解决一下它. 说实话,看原题题面和看CCF代码真是两种完全不同的感受…… ------------思路分析: 把$s$串删去一部分之后,会把$s$串分成两部分,当 ...

  7. Codeforces Round #579 (Div. 3) D2. Remove the Substring (hard version) (思维,贪心)

    题意:给你一个模式串\(t\),现在要在主串\(s\)中删除多个子串,使得得到的\(s\)的子序列依然包含\(t\),问能删除的最长子串长度. 题解:首先,我们不难想到,我们可以选择\(s\)头部到最 ...

  8. sicily 1198. Substring (递归全排列+排序)

    DescriptionDr lee cuts a string S into N pieces,s[1],…,s[N]. Now, Dr lee gives you these N sub-strin ...

  9. salesforce 零基础开发入门学习(二)变量基础知识,集合,表达式,流程控制语句

    salesforce如果简单的说可以大概分成两个部分:Apex,VisualForce Page. 其中Apex语言和java很多的语法类似,今天总结的是一些简单的Apex的变量等知识. 有如下几种常 ...

随机推荐

  1. GitHub从小白到熟悉<二>

    创建 仓库  

  2. python-day31(正式学习)

    一.单机架构 应用领域: 植物大战僵尸 office 二.CS架构 应用领域: QQ 大型网络游戏 计算机发展初期用户去取数据,直接就去主机拿,从这里开始就分出了客户端和服务端. 客户端:用户安装的软 ...

  3. cut,sort,awk,sed,tr,find,wc,uniq在Linux中的用法

    cut语法cut [-bn] [file]cut [-c] [file]cut [-df] [file] -b :以字节为单位进行分割.这些字节位置将忽略多字节字符边界,除非也指定了 -n 标志.-c ...

  4. Python和其他编程语言

    Python和其他编程语言 一.Python介绍 Python的创始人为吉多·范罗苏姆(Guido van Rossum),如下图,少数几个不秃头的语言创始人.1989年的圣诞节期间,Guido为了打 ...

  5. 剑指Offer 1-41 代码(python实现)

    今天主要写了一下offer 1-41题,余下的稍后整理 1 """ 1 镜像二叉树: 递归 """ def mirror(root): if ...

  6. 首个springboot项目总结

    项目说明:采用分布式架构的基于restfulApi的后端微服务api项目. 用到的技术栈:springboot.mybatis.swagger.eureka注册中心.gateway.awss3.doc ...

  7. O008、LVM类型的Storage Pool

    参考https://www.cnblogs.com/CloudMan6/p/5277927.html   LVM类型的Storage Pool   不仅一个文件可以分配给客户机作为虚拟磁盘,宿主机上  ...

  8. 什么是RTTI

    参考:  http://www.cnblogs.com/vamei/archive/2013/04/14/3013985.html 运行时类型识别(RTTI, Run-Time Type Identi ...

  9. git ignore 如何忽略已经提交的文件修改

    git ignore git ignore的作用很简单,本地仓库忽略一些文件的修改. ignore的规格可以按文件匹配,按后缀匹配或者按文件夹匹配. 如果在项目开发过程中,需要忽略某一个文件已经提交的 ...

  10. python: 基本数据类型 与 内置函数 知识整理

    列表 list.append(val) #末尾追加,直接改变无返回 list.inert(2,val) #插入到指定位置 list.extend(mylist1) #list会被改变 list2=li ...