D2. Remove the Substring (hard version)(思维 )
2 seconds
256 megabytes
standard input
standard output
The only difference between easy and hard versions is the length of the string.
You are given a string ss and a string tt, both consisting only of lowercase Latin letters. It is guaranteed that tt can be obtained from ss by removing some (possibly, zero) number of characters (not necessary contiguous) from ss without changing order of remaining characters (in other words, it is guaranteed that tt is a subsequence of ss).
For example, the strings "test", "tst", "tt", "et" and "" are subsequences of the string "test". But the strings "tset", "se", "contest" are not subsequences of the string "test".
You want to remove some substring (contiguous subsequence) from ss of maximum possible length such that after removing this substring tt will remain a subsequence of ss.
If you want to remove the substring s[l;r]s[l;r] then the string ss will be transformed to s1s2…sl−1sr+1sr+2…s|s|−1s|s|s1s2…sl−1sr+1sr+2…s|s|−1s|s| (where |s||s| is the length of ss).
Your task is to find the maximum possible length of the substring you can remove so that tt is still a subsequence of ss.
The first line of the input contains one string ss consisting of at least 11 and at most 2⋅1052⋅105 lowercase Latin letters.
The first line of the input contains one string tt consisting of at least 11 and at most 2⋅1052⋅105 lowercase Latin letters.
It is guaranteed that tt is a subsequence of ss.
Print one integer — the maximum possible length of the substring you can remove so that tt is still a subsequence of ss.
bbaba
bb
3
baaba
ab
2
abcde
abcde
0
asdfasdf
fasd
3
算法:思维
题解:匹配字串问题,我们可以先找到第一个子串出现的位置,记录到一个数组f里面,然后从后往前匹配,每次获取最大的长度,前面那个字符第一次出现的位置,如果a串中有一个字符和b串的字符相等,就向前推一个字符,继续寻找。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath> using namespace std; const int maxn = 2e5+; typedef long long ll; string a;
string b;
int f[maxn]; int main() {
cin >> a >> b;
f[] = -;
int k = ;
for(int i = , j = ; i < a.size(); i++) {
if(j < b.size() && a[i] == b[j]) { //找到第一个子串的位置
f[k++] = i;
j++;
}
}
int ans = ;
for(int i = a.size() - , j = ; i >= ; i--) {
ans = max(ans, i - f[b.size() - j]);
if(j < b.size() && a[i] == b[b.size() - j - ]) {
j++;
}
}
cout << ans << endl;
return ;
}
D2. Remove the Substring (hard version)(思维 )的更多相关文章
- Codeforces Round #579 (Div. 3) D2. Remove the Substring (hard version) (思维,贪心)
题意:给你一个模式串\(t\),现在要在主串\(s\)中删除多个子串,使得得到的\(s\)的子序列依然包含\(t\),问能删除的最长子串长度. 题解:首先,我们不难想到,我们可以选择\(s\)头部到最 ...
- D2. Remove the Substring (hard version)
D2. Remove the Substring (hard version) 给字符串s,t,保证t为s的子序列,求s删掉最长多长的子串,满足t仍为s的子序列 记录t中每个字母在s中出现的最右的位置 ...
- 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 ...
- 双指针(最大删除子串)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$串分成两部分,当 ...
- Remove the Substring
D2. Remove the Substring (hard version) 思路:其实就是贪心吧,先从前往后找,找到 t 可在 s 中存在的最小位置 (pre),再从后往前找,找到 t 可在 s ...
- Codeforces Round #575 (Div. 3) D1+D2. RGB Substring (easy version) D2. RGB Substring (hard version) (思维,枚举,前缀和)
D1. RGB Substring (easy version) time limit per test2 seconds memory limit per test256 megabytes inp ...
- Codeforces Round #575 (Div. 3) D2. RGB Substring (hard version) 水题
D2. RGB Substring (hard version) inputstandard input outputstandard output The only difference betwe ...
随机推荐
- 大型三甲HIS系统最新C#源码医院信息管理系统NET源码门诊住院医嘱药房药库发药管理源码
查看医疗信息管理his系统演示 本云his系统源码,适用在县乡一体化,医联体分级诊疗项目上,本源码开发语言asp.net c#,数据库sqlserver2012,开发工具vs2015.技术框架mvc. ...
- Java序列化流
1.什么是序列化流 序列化就是把Java对象“流化”,序列化后的Java对象可以保存到本地文件系统,或者通过Socket传输到其他的服务器. Java中对象序列化有以下特点: 1)类实现java.io ...
- canvas教程(三) 绘制曲线
经过 canvas 教程(二) 绘制直线 我们知道了 canvas 的直线是怎么绘制的 而本次是给大家带来曲线相关的绘制 绘制圆形 在 canvas 中我们可以使用 arc 方法画一个圆 contex ...
- React 中的 定义组件的 两种方式
React 中创建 Components 的方式有两种:Function and Class 定义一个组件最简单的方法就是写一个 JavaScript 函数 function Welcome(prop ...
- UI5-技术篇-签字板
签字板应用是通过创建自定义控件实现的,相关代码如下: 1.HTML <!DOCTYPE HTML> <html> <head> <meta http-equi ...
- Java8 stream用法-备忘录
1. 如何使用匹配模式 List<String> strs = Arrays.asList("a", "a", "a", &qu ...
- Centos部属前后端项目
一.安装python3 # 下载并解压 cd /opt wget https://www.python.org/ftp/python/3.6.3/Python-3.6.3.tgz tar -zxf P ...
- JQuery EasyUI treegrid展开与折叠,以及数据加载两次的问题
问题:做项目的时候遇到代码生成的页面,只默认展开了一级节点,每次操作之后刷新还要手动一级一级展开,太麻烦了 官方API:http://www.jeasyui.net/plugins/186.html ...
- OPENWRT使用华为 E353/E3131的4G转WIFI路由器作为WAN接口上网(笔记)
参考文档: http://www.yizu.org/archives/721/ 原来使用Hilink模式真的很简单 1.安装一些包: opkg install kmod-usb-net-rndis k ...
- Alternative method of unlocking the bootloader - without code from HUAWEI
原文:https://forum.xda-developers.com/p8lite/general/alternative-method-unlocking-bootloader-t3799294 ...