Record a vegetable-driven growth.
Good Article recommended
Personal feeling is understandable.
This is the case for the 128th week in Hihocoder.
Hihocoder 128th Week
There are also explanations.
Test instructions: There are several different substrings in a string
Answer: Sam is the number of strings in all states. Number of strings per state = (max (s)-min (s) + 1)
Max (s): is the maximum string length that the state s can represent
Min (s) in the same vein.
Write down the simple template here.
#include <bits/stdc++.h> using namespace std;
#define FI First #define SE second typedef long long LL;
const int MAXN = 1000000 + 10;
struct sam{int link[maxn<<1],go[maxn<<1][26],mxlen[maxn<<1],mnlen[maxn<<1];
int tot,last;
void init () {tot = last = 0;
memset (go[0],-1,sizeof (go[0)); Link[0] =-1;
Mxlen[0] = 0;mnlen[0] = 0;
} void ins (int c) {int p = Last,cur = ++tot;
Mxlen[cur] = mxlen[p] + 1;
memset (go[cur],-1,sizeof (go[cur));
while (P! =-1 && go[p][c] = =-1) go[p][c] = Cur,p = Link[p];
if (p = =-1) {mnlen[cur] = 1;
Link[cur] = 0;
} else {int q = go[p][c];
if (mxlen[q] = = Mxlen[p] + 1) mnlen[cur] = Mxlen[q] + 1,link[cur] = q;
else {int NQ = ++tot;
memcpy (go[nq],go[q],sizeof (go[q));
MXLEN[NQ] = mxlen[p] + 1; LINK[NQ] = Link[q];
MNLEN[Q] = Mxlen[nq] + 1; Link[cur] = NQ;
Mnlen[cur] = Mxlen[nq] + 1;
LINK[Q] = NQ; while (P! =-1 && go[p][c] = = q) {Go[p][c] = Nq,p = Link[p];
} MNLEN[NQ] = Mxlen[link[nq]] + 1;
}} last = cur;
}}sam;
Char STR[MAXN];
int main () {while (~SCANF ("%s", str)) {sam.init ();
for (int i = 0; str[i]; i++) Sam.ins (Str[i]-' a ');
LL ans = 0;
for (int i = 1; I <= sam.tot; i++) {ans + sam.mxlen[i]-sam.mnlen[i] + 1;
} printf ("%lld\n", ans);
} return 0; }