其他分享
首页 > 其他分享> > JZOJ 3337. 【NOI2013模拟】wyl8899的TLE

JZOJ 3337. 【NOI2013模拟】wyl8899的TLE

作者:互联网

DescriptionDescriptionDescription

给定AAA 串和BBB串求更改BBB串一位后BBB串的字串是AAA串前缀的最长长度

A,B50000A,B\leq 50000A,B≤50000


SolutionSolutionSolution

写在前面:暴力可过

本人打了hashhashhash+二分代替后缀数组的判断,当然也可以打exkmpexkmpexkmp呢

时间复杂度:O(mlogn)O(mlogn)O(mlogn)


CodeCodeCode

#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);//输出
}

标签:50001,JZOJ,int,mid,long,3337,TLE,ans,maxn
来源: https://blog.csdn.net/xuxiayang/article/details/95209241