• 给定两个字符串A,B
• 再B中删除最少的连续字符(一段字符),使得B成为A的子序列
• 1 ≤ |A|, |B| ≤ 1e5
这题我的想法是这样的:
首先你求一个pre数组和suf数组 pre数组代表第二个串中到目前为止如果可以在第一个串中成为子序列他的从左到右最小下标是多少
suf数组同理代表第二个串中到目前为止如果可以在第一个串中成为子序列他的从右到左最小小标是多少
那么我只要二分开始删除的起点 二分一下右端点 满足 pre[i] != -1 且 pre[i] < suf[右端点] 那么这样他们就能构造成一个子序列
我只要让删除的长度最小 就是我想要的答案
/*
if you can't see the repay
Why not just work step by step
rubbish is relaxed
to ljq
*/
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#include <vector>
#include <stdlib.h>
#include <algorithm>
using namespace std;
#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define dbg2(x1,x2) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<endl
#define dbg3(x1,x2,x3) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<" "<<#x3<<" = "<<x3<<endl
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
typedef pair<int,int> pll;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll _INF = 0xc0c0c0c0c0c0c0c0;
const ll mod = (int)1e9+7;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll ksm(ll a,ll b,ll mod){int ans=1;while(b){if(b&1) ans=(ans*a)%mod;a=(a*a)%mod;b>>=1;}return ans;}
ll inv2(ll a,ll mod){return ksm(a,mod-2,mod);}
void exgcd(ll a,ll b,ll &x,ll &y,ll &d){if(!b) {d = a;x = 1;y=0;}else{exgcd(b,a%b,y,x,d);y-=x*(a/b);}}//printf("%lld*a + %lld*b = %lld\n", x, y, d);
/*namespace sgt
{
#define mid ((l+r)>>1)
#undef mid
}*/
const int MAX_N = 100025;
char str[MAX_N],str_[MAX_N];
int len,len_,pre[MAX_N],suf[MAX_N];
set<int > st[30];
int main()
{
//ios::sync_with_stdio(false);
//freopen("a.txt","r",stdin);
//freopen("b.txt","w",stdout);
string ans= "";
scanf("%s",str+1);
scanf("%s",str_+1);
len = strlen(str+1);
len_ = strlen(str_+1);
memset(pre,-1,sizeof(pre));
memset(suf,-1,sizeof(suf));
for(int i = 1;i<=len;++i)
{
st[str[i]-'a'+1].insert(i);
}
for(int i = 1;i<=26;++i)
{
if(st[i].empty()) continue;
st[i].insert(len+1);
}
int now = 0;
for(int i = 1;i<=len_;++i)
{
set<int>::iterator it ;
if(st[str_[i]-'a'+1].empty()) break;
it = st[str_[i]-'a'+1].lower_bound(now+1);
if(it==st[str_[i]-'a'+1].end()||*it==len+1) break;
else now = *it;
pre[i] = now;
}
now = len+1;
for(int i = len_;i>=1;--i)
{
set<int>::iterator it;
if(st[str_[i]-'a'+1].empty()) break;
it = st[str_[i]-'a'+1].upper_bound(now-1);
if(it==st[str_[i]-'a'+1].begin()) break;
else it--;
now = *it;
suf[i] = now;
}
int minn = len_;
for(int i = 1;i<=len_;++i)
{
if(pre[i]==-1) break;
int l = i+1,r = len_;
while(l<=r)
{
int mid = (l+r)>>1;
if(suf[mid]<=pre[i]||suf[mid]==-1) l = mid + 1;
else r = mid - 1;
}
if(minn>l-i-1)
{
minn = l - i - 1;
ans = "";
for(int j = 1;j<=i;++j) ans+=str_[j];
for(int j = l;j<=len_;++j) ans+=str_[j];
}
}
for(int i = len_;i>=1;--i)
{
if(suf[i]==-1) break;
int l = 1,r = i - 1;
while(l<=r)
{
int mid = (l+r)>>1;
if(pre[mid]>=suf[i]||pre[mid]==-1) r = mid - 1;
else l = mid + 1;
}
if(minn>i-r-1)
{
minn = i - r - 1;
ans = "";
for(int j = 1;j<=r;++j) ans+=str_[j];
for(int j = i;j<=len_;++j) ans+=str_[j];
}
}
if(minn==len_) printf("-");
else printf("%s",ans.c_str());
//fclose(stdin);
//fclose(stdout);
//cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
return 0;
}