双指针(最大删除子串)Codeforces Round #579 (Div. 3)--Remove the Substring (hard version)

时间:2023-12-30 21:27:56

题目链接:https://codeforces.com/contest/1203/problem/D2

题意:

给你S串、T串,问你最长删除多长的子串使得S串里仍然有T的子序列。

思路:

想了好久,先正着跑一下S串,记录T串每一个字符最左边在哪里,再倒着跑一下,记录T串的每一个字符最右边在哪里。

最后跑一下答案:

1. 开头和结尾特判一下,但不是max( L[1]-1 , l1-R[l2] ) , 而是对两个max( L[1]-1 , l1-L[l2]-1 )、max( R[1]-1 , l1-R[l2]-1 )再取max。

2. 对于中间部分:R[i]-L[i-1]-1 。

 #define IOS ios_base::sync_with_stdio(0); cin.tie(0);
#include <cstdio>//sprintf islower isupper
#include <cstdlib>//malloc exit strcat itoa system("cls")
#include <iostream>//pair
#include <fstream>
#include <bitset>
//#include <map>
//#include<unordered_map> https://codeforces.com/contest/1203/problem/D2
#include <vector>
#include <stack>
#include <set>
#include <string.h>//strstr substr
#include <string>
#include <time.h>//srand(((unsigned)time(NULL))); Seed n=rand()%10 - 0~9;
#include <cmath>
#include <deque>
#include <queue>//priority_queue<int, vector<int>, greater<int> > q;//less
#include <vector>//emplace_back
//#include <math.h>
//#include <windows.h>//reverse(a,a+len);// ~ ! ~ ! floor
#include <algorithm>//sort + unique : sz=unique(b+1,b+n+1)-(b+1);+nth_element(first, nth, last, compare)
using namespace std;//next_permutation(a+1,a+1+n);//prev_permutation
#define fo(a,b,c) for(register int a=b;a<=c;++a)
#define fr(a,b,c) for(register int a=b;a>=c;--a)
#define mem(a,b) memset(a,b,sizeof(a))
#define pr printf
#define sc scanf
#define ls rt<<1
#define rs rt<<1|1
void swapp(int &a,int &b);
double fabss(double a);
int maxx(int a,int b);
int minn(int a,int b);
int Del_bit_1(int n);
int lowbit(int n);
int abss(int a);
//const long long INF=(1LL<<60);
const double E=2.718281828;
const double PI=acos(-1.0);
const int inf=(<<);
const double ESP=1e-;
const int mod=(int)1e9+;
const int N=(int)1e6+; int L[N],R[N];
char s[N],t[N]; int main()
{
// freopen("C:\\Users\\13606\\Desktop\\草稿.txt","r",stdin);
int l1,l2;
s[]=t[]='$';
// while(sc("%s%s",s+1,t+1)==2)
// {
sc("%s%s",s+,t+);
l1=strlen(s)-;
l2=strlen(t)-;
for(int i=,pos=;i<=l1&&pos<=l2;++i)
{
if(t[pos]==s[i])
L[pos]=i,pos++;
}
for(int i=l1,pos=l2;i>=&&pos>=;--i)
{
if(t[pos]==s[i])
R[pos]=i,pos--;
}
int ans=;
ans=maxx(ans,maxx(L[]-,R[]-));
ans=maxx(ans,maxx(l1-L[l2],l1-R[l2]));
for(int i=;i<=l2;++i)
ans=maxx(ans,R[i]-L[i-]-);
pr("%d\n",ans);
// }
return ;
} /**************************************************************************************/ int maxx(int a,int b)
{
return a>b?a:b;
} void swapp(int &a,int &b)
{
a^=b^=a^=b;
} int lowbit(int n)
{
return n&(-n);
} int Del_bit_1(int n)
{
return n&(n-);
} int abss(int a)
{
return a>?a:-a;
} double fabss(double a)
{
return a>?a:-a;
} int minn(int a,int b)
{
return a<b?a:b;
}