后缀自动机

后缀自动机

简介

给定一个字符串\(S\),我们把\(S\)的每一个后缀都插入\(trie\)中。这样这个\(trie\)就能处理许多的问题,但这颗\(trie\)的大小是\(O(n^2)\)级别的,所以我们要用到后缀自动机。

构建

定义\(endpos(s)\)为字串\(s\)在原串\(S\)中出现的位置。那不同\(endpos(s)\)的个数为\(O(n)\)级别的,我们可以将一个点看成一个\(endpos(s)\)的等价类来构建。

推荐博客:史上最通俗的后缀自动机详解

P3804 【模板】后缀自动机 (SAM)

考虑建出\(parent-tree\),那么点\(u\)出现的次数就为\(u\)的儿子出现次数之和,但对于一个前缀的贡献是独立的,所以只要在建\(SAM\)时,对每一个前缀加一,求和即可。

\(\text{Code}\)

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 1e6 + 5;
int n,b[N << 2],cnt,h[N << 2]; char s[N];

struct SAM{int fa,len,ch[26];}a[N << 2];
int tot = 1,pre = 1;
void insam(int x)
{
	int u = pre,v; pre = v = ++tot;
	a[v].len = a[u].len + 1, b[v]++;
	for (; u && !a[u].ch[x]; u = a[u].fa) a[u].ch[x] = v;
	if (!u) a[v].fa = 1;
	else
	{
		int g = a[u].ch[x];
		if (a[g].len == a[u].len + 1) a[v].fa = g;
		else
		{
			int now = ++tot;
			a[now] = a[g], a[now].len = a[u].len + 1;
			a[v].fa = a[g].fa = now;
			for (; u && a[u].ch[x] == g; u = a[u].fa) a[u].ch[x] = now;
		}	
	}
}
struct edge{int to,nxt;}e[N << 2];
void add(int x,int y){e[++cnt] = edge{y,h[x]},h[x] = cnt;}
void dfs(int u)
{
	for (int i = h[u]; i; i = e[i].nxt)
		dfs(e[i].to),b[u] += b[e[i].to];
}
int main()
{
	scanf("%s",s + 1),n = strlen(s + 1);
	for (int i = 1; i <= n; i++) insam(s[i] - 'a');
	for (int i = 2; i <= tot; i++) add(a[i].fa,i);
	dfs(1); int ans = 0;
	for (int i = 2; i <= tot; i++)
		if (b[i] > 1) ans = max(ans,a[i].len * b[i]);
	printf("%d\n",ans);
}
posted @ 2022-09-20 16:06  RiverSheep  阅读(23)  评论(0)    收藏  举报