警示后人
查看原帖
警示后人
749630
wxzzzz楼主2024/4/1 22:22

广义 SAM insert 第一次跳后缀链时必须先设 last=sc[last];,因为 t[last][x] 原本就有值,如果不加这一行就根本不会进行跳跃。

inline void insert(ll last,ll x)
{
	rll p=t[last][x],q,nq;
	if(len[p]) return;
	len[p]=len[last]+1;
	last=sc[last];  //←this
	while(last&&!t[last][x])
		t[last][x]=p,last=sc[last];
	if(!last)
	{
		sc[p]=1;
		return;
	}
	q=t[last][x];
	if(len[q]==len[last]+1)
	{
		sc[p]=q;
		return;
	}
	nq=++idt,len[nq]=len[last]+1;
	sc[nq]=sc[q],sc[p]=sc[q]=nq;
	for(rll i=0;i<26;i++)
		if(len[t[q][i]])
			t[nq][i]=t[q][i];
	while(t[last][x]==q)
		t[last][x]=nq,last=sc[last];
}
2024/4/1 22:22
加载中...