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 ...
随机推荐
- python 包 安装 加速 pip anaconda
使用 -i 参数指定源,豆瓣的很快: pip install web.py -i http://pypi.douban.com/simple anaconda: ~/.condarc channels ...
- 页面、 ajax 、mock
页面1: //html <form action = "" method="post" name="loginForm"> & ...
- HTML5 结构标签
一.定义标题栏:header header 元素是一种具有引导和导航作用的结构元素,通常用来放置整个页面或页面内的一个内容区块的标题,但也可以包含其他内容,因此整个页面的标题应该放在页面的开头. he ...
- 关于SqlServer数据库数据备份失败的问题
当备份的失败,出现说什么应该支持多少个介质簇,但实际出现了多少介质簇,这个时候就要考虑备份的地址是不是出现问题. 首先,检查备份地址,是不是多于两个以上,那么在备份的时候应该注意,备份地址最好留一个, ...
- tomcat7.x配置APR高并发模式
Tomcat支持BIO/NIO/APR三种运行模式 ,性能各色春秋! Apr插件提高Tomcat性能 Tomcat可以使用APR来提供超强的可伸缩性和性能,更好地集成本地服务器技术. APR(Apac ...
- selenium 全天课整理(二)
# encoding=utf-8 ''' selenium 全天 二 ''' #unittest例子 from selenium import webdriver import unittest,ti ...
- Flink Streaming基于滚动窗口的事件时间分析
使用flink-1.9.0进行的测试,在不同的并行度下,Flink对事件时间的处理逻辑不同.包括1.1在并行度为1的本地模式分析和1.2在多并行度的本地模式分析两部分.通过理论结合源码进行验证,得到具 ...
- linux系统编程综合练习-实现一个小型的shell程序(三)
上节中已经实现了对普通命令的解析,包括输入重定向,输出重定向,管道,后台作业,这次就来执行已经解析好的命令,对应的函数为:execute_command(),首先对带有管道的命令进行执行: 比如:&q ...
- python统计代码总行数(代码行、空行、注释行)
我们在工作或学习代码的过程中,经常会想知道自己写了多少行代码,今天在项目环境写了个脚本统计了项目代码的数量. 功能: 1.统计代码总行数 2.统计空行数 3.统计注释行数 # coding=utf-8 ...
- Alpha冲刺随笔七:第七天
课程名称:软件工程1916|W(福州大学) 作业要求:项目Alpha冲刺(十天冲刺) 团队名称:葫芦娃队 作业目标:在十天冲刺里对每天的任务进行总结. 随笔汇总:https://www.cnblogs ...