SAM模板

luogu P3804 为例

感觉我对 SAM 理解的不是很透彻。

#include <cstdio>
#include <cstring>
#define R register
#define Null b
#define Max(_A, _B) (_A > _B ? _A : _B)
char s[1000010];
struct Data
{
    int len, size, In;
    Data *ch[26], *pre;
} b[2000010], *last = b, *Q[2000010], **head = Q, **tail = Q;
int tot;
void Add(R int s)
{
    R Data *cur = b + ++tot, *p = last, *q;
    cur->len = last->len + 1;
    while(p && !p->ch[s]) p->ch[s] = cur, p = p->pre;
    if(p == 0) cur->pre = Null;
    else
    {
        q = p->ch[s];
        if(q->len == p->len + 1) cur->pre = q;
        else
        {
            R Data *clone = b + ++tot; *clone = *q;
            clone->len = p->len + 1;
            cur->pre = q->pre = clone;
            clone->size = 0;
            while(p && p->ch[s] == q)
            {
                p->ch[s] = clone;
                p = p->pre;
            }
        }
    }
    cur->size = 1; //只有原有的点有 size
    last = cur;
}
long long Ans;
int main()
{
    scanf("%s", s + 1);
    for(R int i = 1; s[i]; i++) Add(s[i] - 'a');
    for(R Data *i = b + 1; i <= b + tot; i++) i->pre->In++; b[0].In = 19260817;
    for(R Data *i = b + 1; i <= b + tot; i++) if(!i->In) *(++tail) = i;
    do
    {
        ++head;
        (*head)->pre->In--;
        if((*head)->pre->In == 0) *(++tail) = (*head)->pre;
        (*head)->pre->size += (*head)->size;
        if((*head)->size > 1) Ans = Max(1ll * (*head)->size * (*head)->len, Ans);
    }
    while(head < tail);
    printf("%lld\n", Ans);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值