Sphere Longest Common Substring 【后缀自动机+ac自动机】

本文探讨了后缀自动机和AC自动机在查找最长公共子串问题中的应用。作者尝试了一种针对AC自动机的优化方法,理论上可将复杂度降低到O(n+m),但实测中反而导致性能下降。代码实现中,作者发现一处看似冗余的代码,删除后导致答案错误,引发思考。
摘要由CSDN通过智能技术生成

话说这两个自动机真是好机油啊~后缀自动机是另一种意义上的ac自动机~~

不过纠结的是,想了一种用在ac自动机上的优化,有了这种优化后复杂度才是真正的O(n+m),不过加上之后更慢了.....一定是我写水了.....T T

第二个代码是加了那种优化的代码,还有一个地方很纠结,后面后很多///的两行,我感觉可以不要的,可是删了后就wa了!!这不科学。。。

#include<iostream>
#include<vector>
#include<algorithm>
#include<cstdio>
#include<queue>
#include<stack>
#include<string>
#include<map>
#include<set>
#include<cmath>
#include<cassert>
#include<cstring>
#include<iomanip>
using namespace std;
#ifdef _WIN32
#define i64 __int64
#define out64 "%I64d\n"
#define in64 "%I64d"
#else
#define i64 long long
#define out64 "%lld\n"
#define in64 "%lld"
#endif
/************ for topcoder by zz1215 *******************/
#define FOR(i,a,b)      for( int i = (a) ; i <= (b) ; i ++)
#define FFF(i,a)        for( int i = 0 ; i < (a) ; i ++)
#define FFD(i,a,b)      for( int i = (a) ; i >= (b) ; i --)
#define S64(a)          scanf(in64,&a)
#define SS(a)           scanf("%d",&a)
#define LL(a)           ((a)<<1)
#define RR(a)           (((a)<<1)+1)
#define pb              push_back
#define CL(Q)           while(!Q.empty())Q.pop()
#define MM(name,what)   memset(name,what,sizeof(name))
#define MC(a,b)			memcpy(a,b,sizeof(b))
#define MAX(a,b)        ((a)>(b)?(a):(b))
#define MIN(a,b)        ((a)<(b)?(a):(b))
#define read            freopen("in.txt","r",stdin)
#define write           freopen("out.txt","w",stdout)
 
const int inf = 0x3f3f3f3f;
const i64 inf64 = 0x3f3f3f3f3f3f3f3fLL;
const double oo = 10e9;
const double eps = 10e-9;
const double pi = acos(-1.0);
const int maxz = 500011;
const int maxc = 26;
 
int go[maxz][maxc];
int len[maxz];
int f[maxz];
 
struct suffix_automaton
{
	const static int head = 1;
	int use,last;
	
	int get()
	{
		use++;
		MM(go[use],0);	
		return use;
	}
	
	void init()
	{
		use=-1;
		get();get();
		last = head;
		f[0]=len[0]=f[1]=len[1]=0;	
		return ;
	}	
 
	void extend(int c)
	{	
		int end = get();
		int now = last;
		len[end] = len[last]+1;
		last = end;
		for(;now && !go[now][c];now=f[now]) go[now][c]=end;
		if(!now) f[end] = head;
		else
		{
			int to = go[now][c];
			if(len[now]+1==len[to])	f[end]=to;
			else
			{
				int np = get();
				MC(go[np],go[to]);
				f[np] = f[to];
				len[np] = len[now]+1;
				f[to] = f[end] = np;
				for(;now && go[now][c]==to;now=f[now]) go[now][c]=np;
			}
		}
	}	
	
	int ak(string & s)
	{	
		int now=head;
		int c,re=0;
		int to,temp;
		int sp=0;
		for(int i=0;i<s.length();i++)
		{
			c=s[i]-'a';	
			to = go[now][c];
			if(to)
			{
				sp++;
				re = max(re,min(sp,len[to]));
				now = to;
			}
			else
			{
				temp = now;
				while(temp && !go[temp][c])
				{
					temp = f[temp];	
				}	
				if(!temp) 
				{
					sp=0;
					now = head;	
				}
				else
				{	
					sp = min(len[temp],sp);
					temp = go[temp][c];
					now = temp;
					sp++;
					re = max(re,min(len[temp],sp));
				}
			}
		}
		return re;
	}
}SA;
 
int main()
{
	string s,sb;
	cin>>s;
	SA.init();
	for(int i=0;i<s.length();i++)
	{
		SA.extend(s[i]-'a');
	}
	cin>>sb;
	cout<<SA.ak(sb)<<endl;
//	system("pause");
	return 0;
}
#include<iostream>
#include<vector>
#include<algorithm>
#include<cstdio>
#include<queue>
#include<stack>
#include<string>
#include<map>
#include<set>
#include<cmath>
#include<cassert>
#include<cstring>
#include<iomanip>
using namespace std;
#ifdef _WIN32
#define i64 __int64
#define out64 "%I64d\n"
#define in64 "%I64d"
#else
#define i64 long long
#define out64 "%lld\n"
#define in64 "%lld"
#endif
/************ for topcoder by zz1215 *******************/
#define FOR(i,a,b)      for( int i = (a) ; i <= (b) ; i ++)
#define FFF(i,a)        for( int i = 0 ; i < (a) ; i ++)
#define FFD(i,a,b)      for( int i = (a) ; i >= (b) ; i --)
#define S64(a)          scanf(in64,&a)
#define SS(a)           scanf("%d",&a)
#define LL(a)           ((a)<<1)
#define RR(a)           (((a)<<1)+1)
#define pb              push_back
#define CL(Q)           while(!Q.empty())Q.pop()
#define MM(name,what)   memset(name,what,sizeof(name))
#define MC(a,b)			memcpy(a,b,sizeof(b))
#define MAX(a,b)        ((a)>(b)?(a):(b))
#define MIN(a,b)        ((a)<(b)?(a):(b))
#define read            freopen("in.txt","r",stdin)
#define write           freopen("out.txt","w",stdout)

const int inf = 0x3f3f3f3f;
const i64 inf64 = 0x3f3f3f3f3f3f3f3fLL;
const double oo = 10e9;
const double eps = 10e-9;
const double pi = acos(-1.0);
const int maxz = 500011;
const int maxc = 26;

int go[maxz][maxc];
int len[maxz];
int f[maxz];
int down[maxz][maxc];

struct suffix_automaton
{
	const static int head = 1;
	int use,last;
	
	int get()
	{
		use++;
		MM(go[use],0);	
		return use;
	}
	
	void init()
	{
		use=-1;
		get();get();
		last = head;
		f[0]=len[0]=f[1]=len[1]=0;	
		return ;
	}	

	void extend(int c)
	{	
		int end = get();
		int now = last;
		len[end] = len[last]+1;
		last = end;
		for(;now && !go[now][c];now=f[now]) go[now][c]=end;
		if(!now) f[end] = head;
		else
		{
			int to = go[now][c];
			if(len[now]+1==len[to])	f[end]=to;
			else
			{
				int np = get();
				MC(go[np],go[to]);
				f[np] = f[to];
				len[np] = len[now]+1;
				f[to] = f[end] = np;
				for(;now && go[now][c]==to;now=f[now]) go[now][c]=np;
			}
		}
	}	
	
	int ak(string & s)
	{		
		for(int i=1;i<=use;i++)
		{
			for(int j=0;j<26;j++)
			{
				down[i][j] = inf;
			}
		}
		int now=head;
		int c,re=0;
		int to,temp;
		int sp=0;
		for(int i=0;i<s.length();i++)
		{
			c=s[i]-'a';
			to = go[now][c];
			sp = min(sp,len[now]);  ///
			if(to)
			{				
				sp = min(sp,down[now][c]);   
				sp++;
				sp = min(sp,len[to]);
				re = max(re,sp);
				now = to;
			}
			else
			{
				temp = now;
				while(temp && !go[temp][c])
				{
					temp = f[temp];	
				}	
				if(!temp) 
				{
					sp=0;
					go[now][c]=head;
					down[now][c]=0;
					now = head;	
				}
				else
				{	
					sp = min(len[temp],sp);
					down[now][c]=len[temp];
					temp = go[temp][c];	
					go[now][c]=temp;
					now = temp;
					sp++;
					re = max(re,sp);
				}
			}
		}
		return re;
	}
}SA;

int main()
{
	string s,sb;
	cin>>s;
	SA.init();
	for(int i=0;i<s.length();i++)
	{
		SA.extend(s[i]-'a');
	}
	cin>>sb;
	cout<<SA.ak(sb)<<endl;
//	system("pause");
	return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值