Remove the Substring
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的更多相关文章
- 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 ...
- D2. Remove the Substring (hard version)(思维 )
D2. Remove the Substring (hard version) time limit per test 2 seconds memory limit per test 256 mega ...
- D2. Remove the Substring (hard version)
D2. Remove the Substring (hard version) 给字符串s,t,保证t为s的子序列,求s删掉最长多长的子串,满足t仍为s的子序列 记录t中每个字母在s中出现的最右的位置 ...
- 双指针(最大删除子串)Codeforces Round #579 (Div. 3)--Remove the Substring (hard version)
题目链接:https://codeforces.com/contest/1203/problem/D2 题意: 给你S串.T串,问你最长删除多长的子串使得S串里仍然有T的子序列. 思路: 想了好久,先 ...
- Codeforces - 1203D2 - Remove the Substring (hard version) - 双指针
https://codeforces.com/contest/1203/problem/D2 上次学了双指针求两个字符串之间的是否t是s的子序列.但其实这个双指针可以求出的是s的前i个位置中匹配t的最 ...
- CF1203D2 Remove the Substring (hard version) 题解
这题初赛让我白给了6分,于是我决定回来解决一下它. 说实话,看原题题面和看CCF代码真是两种完全不同的感受…… ------------思路分析: 把$s$串删去一部分之后,会把$s$串分成两部分,当 ...
- Codeforces Round #579 (Div. 3) D2. Remove the Substring (hard version) (思维,贪心)
题意:给你一个模式串\(t\),现在要在主串\(s\)中删除多个子串,使得得到的\(s\)的子序列依然包含\(t\),问能删除的最长子串长度. 题解:首先,我们不难想到,我们可以选择\(s\)头部到最 ...
- 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 ...
- salesforce 零基础开发入门学习(二)变量基础知识,集合,表达式,流程控制语句
salesforce如果简单的说可以大概分成两个部分:Apex,VisualForce Page. 其中Apex语言和java很多的语法类似,今天总结的是一些简单的Apex的变量等知识. 有如下几种常 ...
随机推荐
- React 中的函数式思想
函数式编程简要概念 函数式编程中一个核心概念之一就是纯函数,如果一个函数满足一下几个条件,就可以认为这个函数是纯函数了: 它是一个函数(废话): 当给定相同的输入(函数的参数)的时候,总是有相同的输出 ...
- 滚动页面产生动画WOW.js的用法
简介 在一些网页上,当你滚动页面的时候会看到各式各样的元素动画效果,非常动感.WOW.js 就是一款帮助你实现这种 CSS 动画效果的插件.WOW.js 依赖 animate.css,所以它支持 an ...
- 删库?半个DBA的跑路经验总结
0. 国内呆不下了,赶紧出国 首先,不要选动车,要选最近的一班飞机,尽快出国,能走高速走高速,不然选人少的路线. 没错,我们 DBA 都是常备护照的. 切记,注意看高德地图实时路况. 我们有个前辈就是 ...
- centos查看实时网络带宽占用情况方法【转】
Linux中查看网卡流量工具有iptraf.iftop以及nethogs等,iftop可以用来监控网卡的实时流量(可以指定网段).反向解析IP.显示端口信息等. centos安装iftop的命令如下: ...
- git 基础命令 学习总结
首先介绍一个git 里工作流的概念: 你的本地仓库由 git 维护的三棵“树”组成.第一个是你的 工作目录,它持有实际文件:第二个是 缓存区(Index),它像个缓存区域,临时保存你的改动:最后是 H ...
- 自己实现一个简化版的SpringMVC框架
废话不多说,我们进入今天的正题,在Web应用程序设计中,MVC模式已经被广泛使用.SpringMVC以DispatcherServlet为核心,负责协调和组织不同组件以完成请求处理并返回响应的工作,实 ...
- linux系统查看某个用户错误登录次数
pam_tally2 --user user_name 查看user_name用户的错误登录次数 pam_tally2 --user user_name --reset 清空user_name用户的错 ...
- js 简单实现隐藏和显示
<html> <head> <meta charset="gb2312"> <title>隐藏和显示</title> & ...
- Codeforces Round #581 (Div. 2) C. Anna, Svyatoslav and Maps (Floyd 算法,最短路)
C. Anna, Svyatoslav and Maps time limit per test2 seconds memory limit per test256 megabytes inputst ...
- 【HEOI2015】小Z的房间
题意 https://www.luogu.org/problemnew/show/P4111 题解 前置知识:矩阵树定理 不要问证明,我不会,用就完事了(反正一般也不会用到) 因为矩阵树定理就是求一张 ...