题目描述:
请注意:本题简单版和困难版之间的唯一区别是字符串的长度限制。
给你一个字符串 s s s和一个字符串 t t t,两者都只包含小写字母。你可以通过从 s s s中删除一些字符(不必连续,可不删除)而不改变剩余字符的顺序(换句话说,删除一些字符后 t t t仍然是 s s s的子序列),保证最初 t t t是 s s s的子序列。
例如,字符串"test
", “tst
”, “tt
”, “et
"和”“都是字符串"test
"的子序列,而"tset
”, “se
”, "contest
"都不是字符串"test
"的子序列。
您希望从s中删除一些最大可能长度的连续子序列,在删除后t仍将是s的子序列。
如果要删除子串 s [ l ; r ] s[l;r] s[l;r],则原字符串 s s s将变化为 s 1 s 2 . . . s l − 1 s r + 1 s r + 2 . . . s ∣ s ∣ − 1 s ∣ s ∣ s_1s_2...s_{l-1}s_{r+1}s_{r+2}...s_{|s|-1}s_{|s|} s1s2...sl−1sr+1sr+2...s∣s∣−1s∣s∣ ( ∣ s ∣ |s| ∣s∣为字符串 s s s的长度)。
找到可以删除的连续子字符串的最大可能长度,使得删除后 t t t仍将是 s s s的子序列。
输入格式:
第一行包含一个由小写拉丁字母组成的字符串 s s s。第二行包含一个由小写字母组成的字符串 t t t。( 1 ≤ ∣ s ∣ , ∣ t ∣ ≤ 2 × 1 0 5 1\leq |s|,|t|\leq 2\times 10^5 1≤∣s∣,∣t∣≤2×105),保证 t t t是 s s s的子序列。
输出格式:
输出一个整数,即可以删除的子字符串的最大可能长度,使得 t t t仍将是 s s s的子序列。
The only difference between easy and hard versions is the length of the string.
You are given a string s s s and a string t t t , both consisting only of lowercase Latin letters. It is guaranteed that t t t can be obtained from s s s by removing some (possibly, zero) number of characters (not necessary contiguous) from s s s without changing order of remaining characters (in other words, it is guaranteed that t t t is a subsequence of s s 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 s s of maximum possible length such that after removing this substring t t t will remain a subsequence of s s s .
If you want to remove the substring $ s[l;r] $ then the string $ s $ will be transformed to s 1 s 2 … s l − 1 s r + 1 s r + 2 … s ∣ s ∣ − 1 s ∣ s ∣ s_1 s_2 \dots s_{l-1} s_{r+1} s_{r+2} \dots s_{|s|-1} s_{|s|} s1s2…sl−1sr+1sr+2…s∣s∣−1s∣s∣ (where ∣ s ∣ |s| ∣s∣ is the length of s s s ).
Your task is to find the maximum possible length of the substring you can remove so that t t t is still a subsequence of s s s .
The first line of the input contains one string s s s consisting of at least 1 1 1 and at most 2 ⋅ 1 0 5 2 \cdot 10^5 2⋅105 lowercase Latin letters.
The first line of the input contains one string t t t consisting of at least 1 1 1 and at most 2 ⋅ 1 0 5 2 \cdot 10^5 2⋅105 lowercase Latin letters.
It is guaranteed that t t t is a subsequence of s s s .
Print one integer — the maximum possible length of the substring you can remove so that t t t is still a subsequence of s s s .
bbaba
bb
3
baaba
ab
2
abcde
abcde
0
asdfasdf
fasd
3
以上来自洛谷 以上来自洛谷 以上来自洛谷
由于本人较懒,故先打了hard版,再用hard版的代AC了easy版
假设 s s s的长度为 n n n, t t t的长度为 m m m。首先,暴力肯定是不行的了(因为要对标hard版),我们就得考虑找规律。通过推理可以发现,删除的字串大体可以分为三类:
由于要求可删除的子字符串的最大长度,所以要从 s s s的首位开始找,直至找到第一个与 t t t串相同的子字符串出现位置,记录下每一元素位置,并停止寻找。(说得不太清楚,直接看代码自行理解。)
len = 1;
for (int i = 1; i <= len1 && len <= len2; i++) {
if (s[i] == t[len]) {
a1[len++] = i;
}
}
与情况 1 1 1差不多,只是从 s s s末尾开始从后往前找,直至找到第一个与 t t t串相同的子字符串出现位置,记录下每一元素位置,并停止寻找。(看代码比较直观。)
len = len2;
for (int i = len1; i >= 1 && len >= 1; i--) {
if (s[i] == t[len]) {
a2[len--] = i;
}
}
在完成之后, a n s = m a x ( a 2 1 − 1 , l e n 1 − a 1 l e n 2 ) ans = max(a2_1 - 1, len1 - a1_{len2}) ans=max(a21−1,len1−a1len2)
在 s s s中找到最前与最后的与 t t t相同的子字符串后,要删中间的字串就相当于吧最前与最后的子字符串拼接起来。
for (int i = 1; i < len2; i++) {
ans = max(ans, a2[i + 1] - a1[i] - 1);
}
// C++ includes used for precompiling -*- C++ -*-
// Copyright (C) 2003-2013 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR a2 PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// Under Section 7 of GPL version 3, you are granted additional
// permissions described in the GCC Runtime Library Exception, version
// 3.1, as published by the Free Software Foundation.
// You should have received a copy of the GNU General Public License and
// a copy of the GCC Runtime Library Exception along with this program;
// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
// .
/** @file stdc++.h
* This is an implementation file for a precompiled header.
*/
// 17.4.1.2 Headers
// C
#ifndef _GLIBCXX_NO_ASSERT
#include
#endif
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#if __cplusplus >= 201103L
#include
#include
#include
#include
#include
#include
#include
#include
#include
#endif
// C++
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#if __cplusplus >= 201103L
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#endif
using namespace std;
#define int long long
const int Maxn = 2e5 + 5;
char s[Maxn], t[Maxn];
int len1, len2, len, a1[Maxn], a2[Maxn];
int ans;
inline void work() {
cin >> s + 1 >> t + 1;
len1 = strlen(s + 1), len2 = strlen(t + 1);
len = 1;
for (int i = 1; i <= len1 && len <= len2; i++) {
if (s[i] == t[len]) {
a1[len++] = i;
}
}
len = len2;
for (int i = len1; i >= 1 && len >= 1; i--) {
if (s[i] == t[len]) {
a2[len--] = i;
}
}
ans = max(a2[1] - 1, len1 - a1[len2]);
for (int i = 1; i < len2; i++) {
ans = max(ans, a2[i + 1] - a1[i] - 1);
}
cout << ans << endl;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
work();
return 0;
}