这是一个SAM板子

如题

  • 代码
#include<bits/stdc++.h>
using namespace std;
const int N=2e6+5,SZ=26;

struct SuffixAutomaton
{
    struct NODE{
        int ch[SZ],fa;
        int len,right;
    }t[N];
    int root,last,cnt;
    inline int newnode(int _len){
        t[++cnt].len=_len;
        return cnt;
    }
    inline void init(){//"初始化
        root=last=newnode(0);
    }
    inline void ins(int c)//"c=(char)-'a';
    {
        int nq=newnode(t[last].len+1),q=last;
        for(;q&&!t[q].ch[c];q=t[q].fa)t[q].ch[c]=nq;
        if(q==0) t[nq].fa=root;
        else if(t[t[q].ch[c]].len==t[q].len+1)t[nq].fa=t[q].ch[c];
        else{
            int np=newnode(t[q].len+1),p=t[q].ch[c];
            memcpy(t[np].ch,t[p].ch,sizeof(t[p].ch));
            t[np].fa=t[p].fa;
            t[p].fa=t[nq].fa=np;
            for(;q&&t[q].ch[c]==p;q=t[q].fa)t[q].ch[c]=np;
        }
        t[nq].right=1,last=nq;
    }
    int topo[N],buc[N];
    inline void toposet(){
        int mx=0;
        for(int i=1;i<=cnt;i++)buc[t[i].len]++,mx=max(mx,t[i].len);
        for(int i=1;i<=mx;i++)buc[i]+=buc[i-1];
        for(int i=1;i<=cnt;i++)topo[buc[t[i].len]--]=i;
    }
    inline void cal_right()
    {
        toposet();
        for(int i=cnt;i;i--){
            int cur=topo[i];
            t[t[cur].fa].right+=t[cur].right;
        }
    }
}SAM;
char str[N];
int main()
{
    SAM.init();
    scanf("%s",str+1);
    int len=strlen(str+1);
    for(int i=1;i<=len;i++)SAM.ins(str[i]-'a');
    return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值