struct state
{
int len,nex[27],pre;
state(int len=0,int pre=-1):
len(len),pre(pre)
{
memset(nex,0,sizeof(nex));
}
} sam[2000005];
int suffam(char *s)
{
int lenth=strlen(s+1);
int idx=0,last=0;
for(int i=1; i<=lenth; i++)
{
int now=++idx;
int t=s[i]-'a'+1;
sam[now].len=i;
while(~last && !sam[last].nex[t])
{
sam[last].nex[t]=now;
last=sam[last].pre;
}
if(last==-1)
sam[now].pre=0;
else
{
int nex=sam[last].nex[t];
if(sam[last].len+1==sam[nex].len)
sam[now].pre=nex;
else
{
sam[++idx]=state(sam[last].len+1,sam[nex].pre);
for(int i=1;i<=26;i++)
sam[idx].nex[i]=sam[nex].nex[i];
sam[now].pre=sam[nex].pre=idx;
while(~last && sam[last].nex[t]==nex)
sam[last].nex[t]=idx,last=sam[last].pre;
}
}
last=now;
}
return idx;
}
suffix automaton
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
推薦閱讀更多精彩內(nèi)容
- 在無戒老師21天訓(xùn)練營的第七天,我沒堅(jiān)持住日更。 不知道這一批學(xué)員里,還會有多少憧憬寫作的朋友堅(jiān)持不下來。 馬云有...