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 ...
随机推荐
- Java调用WebService方法总结(9,end)--Http方式调用WebService
Http方式调用WebService,直接发送soap消息到服务端,然后自己解析服务端返回的结果,这种方式比较简单粗暴,也很好用:soap消息可以通过SoapUI来生成,也很方便.文中所使用到的软件版 ...
- Dubbo -- 四种loadBalance负载均衡算法
Dubbo中的一个关键接口LoadBalance,dubbo是按照其中的规则来调用多台provider的服务的. 先看一下接口的实现类图: 从上图中我们可以看到dubbo提供了四种算法来实现负载均衡. ...
- js合并多个array
Array.prototype.concat.call(array1, array2, array3, ...)
- JQ分页的使用
<script src="../js/pageMe.js"></script> <script src="../js/comjq.js&qu ...
- @PropertySources和@ImportReSources注解
修改默认加载的配置文件,加载指定的配置文件. @PropertySources 格式:@PropertySources(value={"classpath:xxx.xxx"}) @ ...
- 限制mongoDB内存的方法
docker运行MongoDB,针对于docker容器来进行内存资源的限制 修改MongoDB的运行配置文件,并且重启mongodb storage: dbPath: /var/lib/mongodb ...
- 使用Junit测试框架学习Java
前言 在日常的开发中,离不开单元测试,而且在学习Java时,特别是在测试不同API使用时要不停的写main方法,显得很繁琐,所以这里介绍使用Junit学习Java的方法.此外,我使用log4j将结果输 ...
- 使用Cloudera Manager搭建HDFS完全分布式集群
使用Cloudera Manager搭建HDFS完全分布式集群 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 关于Cloudera Manager的搭建我这里就不再赘述了,可以参考 ...
- zabbix-web切换为nginx及https
目录 zabbix-web切换为nginx及https 1.背景和环境 2.安装nginx 2.1.编译参数 2.2.修改配置文件并配置https 2.3.配置nginx为系统服务 3.安装php 3 ...
- 网卡启动安装dell服务器OS
参照视频 需要将boot改成bois启动 https://www.dell.com/support/contents/cn/zh/cndhs1/videos/videoPlayer/k1ajZzdjo ...