sam模板
SAM模板
struct SAM{ static const int maxn = 300010 * 2; struct node{ node*nxt[26],*fail; int len; }; node*root;int cnt; node no[maxn]; node*newnode(){ return &no[cnt++]; } SAM(){ cnt = 0; root = newnode(); } node* add(node*p,int c){ node*cur = newnode(); cur->len = p->len+1; while(p &&!p->nxt[c])p->nxt[c] = cur,p = p->fail; if(!p){ cur->fail = root;return cur; } node*q = p->nxt[c]; if(q->len == p->len+1){ cur->fail = q; }else{ node*nq = newnode(); *nq = *q; nq->len = p->len+1; q->fail = cur->fail = nq; while(p&&p->nxt[c]==q)p->nxt[c] = nq,p = p->fail; } return cur; } ll getNumOfDistinctSubstrings(){ auto ans = 0; REP(i,1,cnt)ans+=no[i].len-no[i].fail->len; return (ans); } };