//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 的格式要求是 "插入 [该代码片段的中文名]"

{
    "AC自动机": {
        "prefix": "acm-6.6Trie图",
        "body": [
            "#include<algorithm>",
            "#include<cstring>",
            "#include<cstdio>",
            "#include<queue>",
            "#define Re register int",
            "#define LL long long",
            "using namespace std;",
            "const int N=2e5+3;",
            "int n;char ch[N*10];",
            "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 AC_Automaton{",
            "\tint O,edp[N],fail[N],tr[N][26];queue<int>Q;",
            "\tAC_Automaton(){O=1;}",
            "\tinline void insert(char ch[],Re id){",
            "\t\tRe p=1;",
            "\t\tfor(Re i=1;ch[i];++i){",
            "\t\t\tRe a=ch[i]-'a';",
            "\t\t\tif(!tr[p][a])tr[p][a]=++O;",
            "\t\t\tp=tr[p][a];",
            "\t\t}",
            "\t\tedp[id]=p;",
            "\t}",
            "\tinline void get_fail(){",
            "\t\tfor(Re i=0;i<26;++i)tr[0][i]=1;",
            "\t\tQ.push(1);",
            "\t\twhile(!Q.empty()){",
            "\t\t\tRe x=Q.front();Q.pop();",
            "\t\t\tfor(Re i=0;i<26;++i)",
            "\t\t\t\tif(tr[x][i])fail[tr[x][i]]=tr[fail[x]][i],Q.push(tr[x][i]);",
            "\t\t\t\telse tr[x][i]=tr[fail[x]][i];",
            "\t\t}",
            "\t}",
            "\tint ru[N],cnt[N];",
            "\tinline void sakura(char ch[]){",
            "\t\tfor(Re i=1,p=1;ch[i];++i)p=tr[p][ch[i]-'a'],++cnt[p];",
            "\t\tfor(Re i=2;i<=O;++i)++ru[fail[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\tcnt[fail[x]]+=cnt[x];",
            "\t\t\tif(!(--ru[fail[x]]))Q.push(fail[x]);",
            "\t\t}",
            "\t\tfor(Re i=1;i<=n;++i)printf(\"%d\\n\",cnt[edp[i]]);",
            "\t}",
            "}AC;",
            "int main(){",
            "//\tfreopen(\"123.txt\",\"r\",stdin);",
            "\tin(n);",
            "\tfor(Re i=1;i<=n;++i)scanf(\"%s\",ch+1),AC.insert(ch,i);",
            "\tAC.get_fail();",
            "\tscanf(\"%s\",ch+1);AC.sakura(ch);",
            "}",
            ""
        ],
        "description": "插入 AC自动机"
    }
}