D2. Remove the Substring (hard version)

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 second 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

Copy

bbaba
bb

output

Copy

3

input

Copy

baaba
ab

output

Copy

2

input

Copy

abcde
abcde

output

Copy

0

input

Copy

asdfasdf
fasd

output

Copy

3

一共有三种情况,

首字符出现最晚 (1,i)

末字符出现最早 (末字符,n)

在两个字符之间最长 下一个匹配的字母使其最远为j(i,j)

下一个字母最晚在哪里匹配,可以倒着求一遍t串的最快匹配。

#include<iostream>
#include<algorithm>
#include<vector>
#include<cstring>
#include<set>
#include<queue>
using namespace std;
const int maxn = 2e5 + 10;
char s[maxn], t[maxn];
int pre[maxn], last[maxn], ls, lt, ans;
void head()
{
	int x = 1;
	for (int i = 1; i <= ls; i++)
	{
		if (s[i] == t[x]) pre[x++]=i;//x字符最快匹配位置
		if (x > ls)return;
	}
}
void end()
{
	int x = lt;
	for (int i = ls; i >= 1; i--)
	{
		if (s[i] == t[x]) last[x--] = i;//x字符最晚匹配位置
		if (x < 1)return;
	}
}
int main()
{
	cin >> (s + 1) >> (t + 1);
	ls = strlen(s + 1), lt = strlen(t + 1);
	head();
	end();
	ans = max(last[1] - 1, ls - pre[lt]);
	for (int i = 1; i < lt; i++)
	{
		ans = max(ans, last[i+1] - pre[i]-1);//这里的last[i+1]意义是,当匹配到i字符时,i的下一个字符的最晚匹配位置。
	}
	cout << ans;
}

但是交G++20会ce,G++17就AC

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值