广义后缀自动机小结

一点点正题

其实这个没有太多可讲的,就是一个将所有字符串建在同一个SAM上的技巧。

举个栗子:给出多个字符串,问其中包含多少个本质不同的子串。

这个时候建一个广义SAM就很好解决,具体就是每次插入一个字符串时,将 l a s t last last 设为 0 0 0(即初始状态)。

避免歧义,给一下自己的代码:

struct state{
	int len,link;
	map<char,int>next;
}st[maxn];
int id=0,last,now,p,q;
void extend(char x)
{
	now=++id;
	st[now].len=st[last].len+1;
	for(p=last;p!=-1&&!st[p].next.count(x);p=st[p].link)st[p].next[x]=now;
	if(p!=-1)
	{
		q=st[p].next[x];
		if(st[p].len+1==st[q].len)st[now].link=q;
		else
		{
			int clone=++id;
			st[clone]=st[q];st[clone].len=st[p].len+1;
			for(;p!=-1&&st[p].next[x]==q;p=st[p].link)st[p].next[x]=clone;
			st[q].link=st[now].link=clone;
		}
	}
	last=now;
}

int main()
{
	scanf("%d",&n);
	st[0].link=-1;
	for(int i=1,x;i<=n;i++)
	{
		scanf("%s",s+1);x=strlen(s+1);last=0;//所谓的last设为0
		for(int j=1;j<=x;j++)extend(s[j],i);
	}
}

这样搞的话,这个后缀自动机就能够同时处理多个字符串的子串问题了。

比如说,对于上面那个例子,只需要加上下面这一行,就可以统计出答案。

for(int i=1;i<=id;i++)ans+=st[i].len-st[st[i].link].len;

没错,用起来跟平常的SAM是没有太大区别的qwq。

以及,在插入一个新的串时,这个串可能作为前面的串的子串出现过,这就导致了自动机上已经有对应状态的节点了,此时就不需要新建一个节点,不然新建的节点有时就是废的。

对于一些不需要这些额外点的题,可以考虑不将他们加入SAM中,在前面加这一段代码即可,意义很明显就不解释了:

if(st[last].next[x])
{
	p=last;q=st[p].next[x];
	if(st[p].len+1==st[q].len)last=q;
	else
	{
		int clone=++id;
		st[clone]=st[q];st[clone].len=st[p].len+1;
		for(;p!=-1&&st[p].next[x]==q;p=st[p].link)st[p].next[x]=clone;
		st[q].link=clone; last=clone;
	} 
	return;
}

模板题

代码如下:

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define maxn 2000010

int n,m; char s[maxn];
struct state{int len,link,next[26];}st[maxn];
int id=0,last,now,p,q;
void extend(int x)
{
	if(st[last].next[x])//加不加这一段无所谓,但是加了能跑快一点点
	{
		p=last;q=st[p].next[x];
		if(st[p].len+1==st[q].len)last=q;
		else
		{
			int clone=++id;
			st[clone]=st[q];st[clone].len=st[p].len+1;
			for(;p!=-1&&st[p].next[x]==q;p=st[p].link)st[p].next[x]=clone;
			st[q].link=clone; last=clone;
		} 
		return;
	}
	now=++id;st[now].len=st[last].len+1;
	for(p=last;p!=-1&&!st[p].next[x];p=st[p].link)st[p].next[x]=now;
	if(p!=-1)
	{
		q=st[p].next[x];
		if(st[p].len+1==st[q].len)st[now].link=q;
		else
		{
			int clone=++id;
			st[clone]=st[q];st[clone].len=st[p].len+1;
			for(;p!=-1&&st[p].next[x]==q;p=st[p].link)st[p].next[x]=clone;
			st[q].link=st[now].link=clone;
		}
	}last=now;
}

int main()
{
	scanf("%d",&n);st[0].link=-1;for(int i=1;i<=n;i++){
		scanf("%s",s+1);m=strlen(s+1);last=0;
		for(int j=1;j<=m;j++)extend(s[j]-'a');
	}
	long long ans=0;
	for(int i=1;i<=id;i++)ans+=st[i].len-st[st[i].link].len;
	printf("%lld",ans);
}

顺手贴一点题,以后可能还会更新?

USACO 17DEC Standing Out from the Herd P   题解
[ZJOI2015]诸神眷顾的幻想乡   题解
SP8093 JZPGYZ - Sevenk Love Oimaster   题解
CF666E Forensic Examination   题解
bzoj 5408: string   题解

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值