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

{
    "喵星球上的点名": {
        "prefix": "acm-6.6广义SAM",
        "body": [
            "#include<algorithm>",
            "#include<cstring>",
            "#include<cstdio>",
            "#include<queue>",
            "#include<map>",
            "#define Re register int",
            "#define LL long long",
            "using namespace std;",
            "const int N=2e5+5;",
            "int n,t,T,x,s[N],ch[N],Len[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 Suffix_Automaton{\t",
            "\tint O,pos[N],cnt[N],link[N],maxlen[N],minlen[N];queue<int>Q;map<int,int>trans[N];",
            "\tSuffix_Automaton(){O=1;}",
            "\tinline int insert(Re ch,Re last){",
            "\t\tif(trans[last][ch]&&maxlen[last]+1==maxlen[trans[last][ch]])return trans[last][ch];",
            "\t\tRe x,y,z=++O,p=last,flag=0;maxlen[z]=maxlen[last]+1;",
            "\t\twhile(p&&!trans[p][ch])trans[p][ch]=z,p=link[p];",
            "\t\tif(!p)link[z]=1;",
            "\t\telse{",
            "\t\t\tx=trans[p][ch];",
            "\t\t\tif(maxlen[p]+1==maxlen[x])link[z]=x;",
            "\t\t\telse{",
            "\t\t\t\tif(maxlen[p]+1==maxlen[z])flag=1;",
            "\t\t\t\ty=++O;maxlen[y]=maxlen[p]+1;",
            "\t\t\t\ttrans[y]=trans[x];",
            "\t\t\t\twhile(p&&trans[p][ch]==x)trans[p][ch]=y,p=link[p];",
            "\t\t\t\tlink[y]=link[x],link[z]=link[x]=y;",
            "\t\t\t}",
            "\t\t}",
            "\t\treturn flag?y:z;",
            "\t}",
            "\tint co[N];",
            "\tinline void updata(Re p,Re id){",
            "\t\twhile(p&&co[p]!=id)++cnt[p],co[p]=id,p=link[p];",
            "\t}",
            "\tint gs[N],Ans[N];",
            "\tinline void updata_(Re p,Re id){",
            "\t\twhile(p&&co[p]!=id)Ans[id]+=gs[p],co[p]=id,p=link[p];",
            "\t}",
            "\tinline void ask(Re ch[],Re L){",
            "\t\tRe p=1,flag=1;",
            "\t\tfor(Re i=1;i<=L&&flag;++i){",
            "\t\t\tRe a=ch[i];",
            "\t\t\tif(trans[p][a])p=trans[p][a];",
            "\t\t\telse flag=0;",
            "\t\t}",
            "\t\tif(flag)++gs[p];",
            "\t\tprintf(\"%d\\n\",flag?cnt[p]:0);",
            "\t}",
            "}SAM;",
            "int main(){",
            "//\tfreopen(\"123.txt\",\"r\",stdin);",
            "\tin(n),in(T);",
            "\tfor(Re i=1;i<=n;++i){",
            "\t\tin(Len[(i<<1)-1]);Re last=1;",
            "\t\tfor(Re j=1;j<=Len[(i<<1)-1];++j)in(x),last=SAM.insert(s[++t]=x,last);",
            "\t\tin(Len[i<<1]);last=1;",
            "\t\tfor(Re j=1;j<=Len[i<<1];++j)in(x),last=SAM.insert(s[++t]=x,last);",
            "\t}",
            "\tfor(Re i=1,t=0;i<=n;++i){",
            "\t\tfor(Re j=1,p=1;j<=Len[(i<<1)-1];++j)",
            "\t\t\tSAM.updata(p=SAM.trans[p][s[++t]],i);",
            "\t\tfor(Re j=1,p=1;j<=Len[i<<1];++j)",
            "\t\t\tSAM.updata(p=SAM.trans[p][s[++t]],i);",
            "\t}",
            "\twhile(T--){",
            "\t\tin(Len[0]);",
            "\t\tfor(Re i=1;i<=Len[0];++i)in(ch[i]);",
            "\t\tSAM.ask(ch,Len[0]);",
            "\t}",
            "\tmemset(SAM.co,0,sizeof(SAM.co));",
            "\tfor(Re i=1,t=0;i<=n;++i){",
            "\t\tfor(Re j=1,p=1;j<=Len[(i<<1)-1];++j)",
            "\t\t\tSAM.updata_(p=SAM.trans[p][s[++t]],i);",
            "\t\tfor(Re j=1,p=1;j<=Len[i<<1];++j)",
            "\t\t\tSAM.updata_(p=SAM.trans[p][s[++t]],i);",
            "\t}",
            "\tfor(Re i=1;i<=n;++i)printf(\"%d \",SAM.Ans[i]);",
            "}",
            ""
        ],
        "description": "插入 喵星球上的点名"
    }
}