D2. Remove the Substring (hard version)
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

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.

Input

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.

Output

Print one integer — the maximum possible length of the substring you can remove so that tt is still a subsequence of ss.

Examples
input
bbaba
bb
output
3
input
baaba
ab
output
2
input
abcde
abcde
output
0
input
asdfasdf
fasd
output
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)(思维 )的更多相关文章

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

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

  2. D2. Remove the Substring (hard version)

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

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

  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. Remove the Substring

    D2. Remove the Substring (hard version) 思路:其实就是贪心吧,先从前往后找,找到 t 可在 s 中存在的最小位置 (pre),再从后往前找,找到 t 可在 s ...

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

  9. Codeforces Round #575 (Div. 3) D2. RGB Substring (hard version) 水题

    D2. RGB Substring (hard version) inputstandard input outputstandard output The only difference betwe ...

随机推荐

  1. 兴奋与沮丧并存spider爬取拉勾网

    兴奋的开发除了爬取拉勾网的爬虫信息,可是当调试都成功了的那一刻,我被拉钩封IP了. 下面是spider的主要内容 import reimport scrapy from bs4 import Beau ...

  2. 关于素数表-C++

    废话不多说,先贴代码: #include <iostream> using namespace std; bool is_prime(int n) { || n == ) return f ...

  3. 在js中把json中的 key去掉双引号的方法

    方法一: //数据格式是这样的: var data = '[{"id":30348079,"name":"表1","score&q ...

  4. DQL 查询表中的数据

    DQL 查询表中的数据:查询语句(最复杂的语句)不会对数据库中的数据进行修改,只是一种显示数据的方式 语法格式: select 字段列表 from 表名列表 where 条件列表 group by 分 ...

  5. Oracle 用户模式

    在 Oracle 数据库中,为了便于管理用户所创建的数据库对象(数据表.索引.视图等),引入了模式的概念,这样某个用户所创建的数据库对象就都属于该用户模式. 一.模式与模式对象 模式是一个数据库对象的 ...

  6. hybris commerce storefront的产品搜索功能

    在Hybris Commerce Cloud的storefront的搜索栏键入一些字母,每次键入,会触发一个发送到后台的http请求实现live search的功能: http url如下:https ...

  7. 在SAP Hybris commerce Storefront里购物下单

    操作过程和大家平时在网上购物没有太大差别. 选中一款心仪的产品,点击Add to cart加到购物车里: 点击Check out结帐: 生成一个购物车ID: 维护发货地址: 维护发货方式: 点击Pla ...

  8. MySQL DataType--日期格式

    在MySQL中,无论是字符串转换为时间还是时间转换为字符串,都需要使用到时间格式: %a 缩写星期名 %b 缩写月名 %c 月,数值 %D 带有英文前缀的月中的天 %d 月的天,数值(00-31) % ...

  9. Python——模块合集

    标准库模块 ● Python——OS(系统操作模块) ● Python——MD5(加密模块) ● Python——time(时间模块) ● Python——re(正则表达式) ● Python——sy ...

  10. 大数据技术之Hadoop3.1.2版本伪分布式部署

    大数据技术之Hadoop3.1.2版本伪分布式部署 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.主机环境准备 1>.操作系统环境 [root@node101.yinzh ...