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 ;
}

最新文章

  1. [译] 企业级 OpenStack 的六大需求(第 3 部分):弹性架构、全球交付
  2. HTML常用命名和CSS reset代码【收集总结】
  3. dom4j操作xml
  4. CSS长度单位
  5. 错误 1 无法嵌入互操作类型“Microsoft.Office.Interop.Excel.ApplicationClass”。请改用适用的接口
  6. sql连接查询 2011-10-10 23:13 (QQ空间)
  7. Android应用程序基础
  8. JavaWeb 后端 &lt;四&gt; 之 Cookie HttpSession 学习笔记
  9. AngularJS 和 Bootstrap
  10. css3文字与字体样式
  11. 【2017集美大学1412软工实践_助教博客】团队作业4——第一次项目冲刺(Alpha版本)小组 成绩
  12. Java I/O---字符与字节转换流---FileReader&amp;FileWriter:
  13. RabbitMQ之php-amqplib使用
  14. LIN 笔记
  15. 【Linux】Centos之安装Nginx及注意事项
  16. which/whereis/locate/find的区别
  17. Asterisk1.8 sip编码协商分析
  18. WITH HINDSIGHT
  19. tp between
  20. innodb 行级锁

热门文章

  1. 【转载】C#中Add方法将往List集合末尾添加相应元素对象
  2. Jmeter获取数据库数据
  3. group by 和 order by 的区别 + 理解过程
  4. Linux expect实现自动登录
  5. oracle 中查询当前用户可以看到的表名、表对应的所有字段 原
  6. vs2017开启JavaScript智能提示
  7. Redis 学习-持久化与主从复制
  8. Android笔记(三十六) AsyncTask是如何执行的?
  9. zabbix监控内存
  10. Oracle SQL developer客户端 如何连接已经安装完毕的Oracle服务器端