Description
给定A 串和B串求更改B串一位后B串的字串是A串前缀的最长长度
A,B≤50000
Solution
写在前面:暴力可过
本人打了hash+二分代替后缀数组的判断,当然也可以打exkmp呢
时间复杂度:O(mlogn)
Code
#pragma GCC optimize("Ofast")
#pragma GCC optimize("inline")
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;char a[50001],b[50001];int n,m,k=-1,ans,maxn;
unsigned long long power[50001],h1[50001],h2[50001];
inline unsigned long long hs1(int l,int r){return h1[r]-h1[l-1]*power[r-l+1];}
inline unsigned long long hs2(int l,int r){return h2[r]-h2[l-1]*power[r-l+1];}
int main()
{
scanf("%s%s",a+1,b+1);
n=strlen(a+1);m=strlen(b+1);
power[0]=1;
for(register int i=1;i<=n;i++)
{
power[i]=power[i-1]*233;
h1[i]=h1[i-1]*233+a[i]-'a';
}
for(register int i=1;i<=m;i++) h2[i]=h2[i-1]*233+b[i]-'a';
for(register int i=1,ans,l,r;i<=m;i++)
{
l=0;r=min(n,m-i+1);//先找最长前缀
while(l<=r)
{
int mid=l+r>>1;
if(h1[mid]==hs2(i,i+mid-1)) l=mid+1;
else r=mid-1;
}
ans=r+1;l=r+1;r=min(n,m-i+1);
while(l<=r)//忽略掉不相同的一位后再找一遍
{
int mid=l+r>>1;
if(hs1(ans+1,mid)==hs2(i+ans,i+mid-1)) l=mid+1;
else r=mid-1;
}
maxn=max(maxn,r);
}
printf("%d",maxn);//输出
}