//Place your snippets for cpp here. Each snippet is defined under a snippet name and has a prefix, body and 
// description. The prefix is what is used to trigger the snippet and the body will be expanded and inserted. Possible variables are:
// $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders. Placeholders with the 
// same ids are connected.
// prefix 的格式要求是 acm-[一级标题编号].[二级标题编号][该代码片段的英文简称]
// description 的格式要求是 "插入 [该代码片段的中文名]"

{
    "广义SAM": {
        "prefix": "acm-6.6离线dfs",
        "body": [
            "#include<algorithm>",
            "#include<cstdio>",
            "#include<queue>",
            "#define Re register int",
            "#define LL long long",
            "using namespace std;",
            "const int N=2e6+5,M=1e6+3;",
            "int n,t;char ch[N];",
            "inline void in(Re &x){",
            "    int fu=0;x=0;char c=getchar();",
            "    while(c<'0'||c>'9')fu|=c=='-',c=getchar();",
            "    while(c>='0'&&c<='9')x=(x<<1)+(x<<3)+(c^48),c=getchar();",
            "    x=fu?-x:x;",
            "}",
            "struct Trie{",
            "    int O,c[M],fa[M],tr[M][26];",
            "    Trie(){O=1;}",
            "    inline void insert(char ch[]){",
            "        Re p=1;",
            "        for(Re i=1;ch[i];++i){",
            "            Re a=ch[i]-'a';",
            "            if(!tr[p][a])tr[p][a]=++O,fa[O]=p,c[O]=a;",
            "            p=tr[p][a];",
            "        }",
            "    }",
            "}T1;",
            "struct Suffix_Automaton{",
            "    int O,pos[N],link[N],maxlen[N],trans[N][26];queue<int>Q;",
            "    //trans[i]: 状态转移指针",
            "    Suffix_Automaton(){O=1;}",
            "    inline int insert(Re ch,Re last){",
            "        Re x,y,z=++O,p=last;maxlen[z]=maxlen[last]+1;",
            "        while(p&&!trans[p][ch])trans[p][ch]=z,p=link[p];",
            "        if(!p)link[z]=1;",
            "        else{",
            "            x=trans[p][ch];",
            "            if(maxlen[p]+1==maxlen[x])link[z]=x;",
            "            else{",
            "                y=++O;maxlen[y]=maxlen[p]+1;",
            "                for(Re i=0;i<26;++i)trans[y][i]=trans[x][i];",
            "                while(p&&trans[p][ch]==x)trans[p][ch]=y,p=link[p];",
            "                link[y]=link[x],link[z]=link[x]=y;",
            "            }",
            "        }",
            "        return z;",
            "    }",
            "    inline void dfs(Re x){",
            "\t\tfor(Re i=0,to;i<26;++i)if(to=T1.tr[x][i])",
            "\t\t\tpos[to]=insert(T1.c[to],pos[x]),dfs(to);",
            "\t}",
            "    inline void build(){pos[1]=1,dfs(1);}",
            "    inline void sakura(){",
            "        LL ans=0;",
            "        for(Re i=2;i<=O;++i)ans+=maxlen[i]-maxlen[link[i]];",
            "        printf(\"%lld\\n\",ans);",
            "    }",
            "}SAM;",
            "int main(){",
            "//    freopen(\"123.txt\",\"r\",stdin);",
            "    in(n);",
            "    for(Re i=1;i<=n;++i)scanf(\"%s\",ch+1),T1.insert(ch);",
            "    SAM.build(),SAM.sakura();",
            "}",
            ""
        ],
        "description": "插入 广义SAM"
    }
}