话说这两个自动机真是好机油啊~后缀自动机是另一种意义上的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;
}