Codeforces 1203D2

D2. Remove the Substring (hard version)

The only difference between easy and hard versions is the length of the string.

You are given a string s and a string t, both consisting only of lowercase Latin letters. It is guaranteed that t can be obtained from s by removing some (possibly, zero) number of characters (not necessary contiguous) from s without changing order of remaining characters (in other words, it is guaranteed that t is a subsequence of s).

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 s of maximum possible length such that after removing this substring t will remain a subsequence of s.

If you want to remove the substring s[l;r] then the string s will be transformed to s1s2…sl−1sr+1sr+2…s|s|−1s|s| (where |s| is the length of s).

Your task is to find the maximum possible length of the substring you can remove so that t is still a subsequence of s.

Input

The first line of the input contains one string s consisting of at least 1 and at most 2⋅105 lowercase Latin letters.

The first line of the input contains one string t consisting of at least 1 and at most 2⋅105 lowercase Latin letters.

It is guaranteed that t is a subsequence of s.

Output

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

Examples

input
bbaba
bb
output
3

input
baaba
ab
output
2

input
abcde
abcde
output
0

input
asdfasdf
fasd
output
3

思路

题目意思是让我们从字符串s中切割一个尽可能大的区间,使得t仍然为剩下的字符串的子序列。先将s从前、从后都遍历一遍,分别记录t的每个字符在s中的位置,t每个字符都遍历完则结束。从左开始遍历,则最右字符的右区间为最大右区间;从右开始遍历,则最左字符的左区间为最大左区间。再逐个枚举中间的相邻两个字符作为分割点,求出中间的最大区间。将三个区间进行比较。
eg :

asdfadbcfbsc
adbc

L[0] = 0
L[1] = 2
L[2] = 6
L[3] = 7

R[0] = 11
R[1] = 9
R[2] = 5
R[3] = 4

代码

#include<iostream>
#include<cstring>
using namespace std;
const int maxn = 2e5+10;

char s[maxn],t[maxn];
int l[maxn],r[maxn];

int main() {
	cin >> s >> t;
	int s_len = strlen(s);
	int t_len = strlen(t);
	int lcnt = 0, rcnt = 0;
	for(int i=0;i<s_len;i++) {
		if(s[i] == t[lcnt]) {
			l[lcnt] = i;
			lcnt++;
		}
		if(lcnt == t_len) {
			break;
		}
	}
	for(int i=s_len-1;i>=0;i--) {
		if(s[i] == t[t_len-rcnt-1]) {
			r[rcnt] = i;
			rcnt++;
		}
		if(rcnt == t_len) {
			break;
		}
	}
	int ans = max(r[t_len-1],s_len-l[t_len-1]-1);
	for(int i=0;i<t_len-1;i++) {
		ans = max(ans,r[i]-l[t_len-i-2]-1);
	}
	cout << ans << endl;
}

/*
asdfadbcfbsc
adbc

answer: 6


aaaabbbb
ab

answer: 6
*/

题目来源

Codeforces 1203D2

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值