//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.6Suffix_Automaton",
        "body": [
            "#include<algorithm>",
            "#include<cstdio>",
            "#include<queue>",
            "#define Re register int",
            "#define LL long long",
            "using namespace std;",
            "const int N=2e6+5;",
            "char ch[2000003];",
            "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 Suffix_Automaton{",
            "\tint O,last,ru[N],siz[N],link[N],maxlen[N],minlen[N],trans[N][26];queue<int>Q;",
            "\tSuffix_Automaton(){last=O=1;}",
            "\tinline void insert(char ch){",
            "\t\tRe z=++O,p=last;siz[z]=1,maxlen[z]=maxlen[last]+1;",
            "\t\twhile(p&&!trans[p][ch])trans[p][ch]=z,p=link[p];",
            "\t\tif(!p)link[z]=1;//û�ҵ�trans[...][ch]",
            "\t\telse{",
            "\t\t\tRe x=trans[p][ch];",
            "\t\t\tif(maxlen[p]+1==maxlen[x])link[z]=x;",
            "\t\t\telse{",
            "\t\t\t\tRe y=++O;maxlen[y]=maxlen[p]+1;",
            "\t\t\t\t",
            "\t\t\t\tfor(Re i=0;i<26;++i)trans[y][i]=trans[x][i];",
            "\t\t\t\twhile(p&&trans[p][ch]==x)trans[p][ch]=y,p=link[p];",
            "\t\t\t\tlink[y]=link[x];",
            "\t\t\t\t",
            "\t\t\t\tlink[z]=link[x]=y;",
            "\t\t\t}",
            "\t\t}",
            "\t\tlast=z;",
            "\t}",
            "\tinline void Tuopu(){",
            "\t\tfor(Re i=2;i<=O;++i)++ru[link[i]];",
            "\t\tfor(Re i=1;i<=O;++i)if(!ru[i])Q.push(i);",
            "\t\twhile(!Q.empty()){",
            "\t\t\tRe x=Q.front();Q.pop();",
            "\t\t\tsiz[link[x]]+=siz[x];",
            "\t\t\tif(!(--ru[link[x]]))Q.push(link[x]);",
            "\t\t}",
            "\t}",
            "\tinline void sakura(){",
            "\t\tfor(Re i=2;i<=O;++i)minlen[i]=maxlen[link[i]]+1;",
            "\t\tTuopu();Re ans=0;",
            "\t\tfor(Re i=1;i<=O;++i)if(siz[i]>1)ans=max(ans,siz[i]*maxlen[i]);",
            "\t\tprintf(\"%d\\n\",ans);",
            "\t}",
            "}SAM;",
            "int main(){",
            "//\tfreopen(\"123.txt\",\"r\",stdin);",
            "\tscanf(\"%s\",ch+1);",
            "\tfor(Re i=1;ch[i];++i)SAM.insert(ch[i]-'a');",
            "\tSAM.sakura();",
            "}",
            ""
        ],
        "description": "插入 SAM后缀自动机"
    }
}