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

{
    "LCT动态维护SAM": {
        "prefix": "acm-6.6Substring[Bzoj2555]",
        "body": [
            "#include<algorithm>",
            "#include<cstring>",
            "#include<cstdio>",
            "#include<string>",
            "#include<queue>",
            "#define LL long long",
            "#define Re register int",
            "using namespace std;",
            "const int N=12e5+3;",
            "int T,ans,mask;char op[10],s[N];",
            "inline void in(Re &x){",
            "    int f=0;x=0;char c=getchar();",
            "    while(c<'0'||c>'9')f|=c=='-',c=getchar();",
            "    while(c>='0'&&c<='9')x=(x<<1)+(x<<3)+(c^48),c=getchar();",
            "    x=f?-x:x;",
            "}",
            "inline string In(Re mask){",
            "    scanf(\"%s\",s);string chars=s;",
            "    for(int j=0;j<chars.length();j++){",
            "        mask=(mask*131+j)%chars.length();",
            "        char t=chars[j];",
            "        chars[j]=chars[mask];",
            "        chars[mask]=t;",
            "    }",
            "    return chars;",
            "}",
            "struct Suffix_Automaton{",
            "\tstruct Link_Cut_Tree{",
            "\t\t#define pl tr[p].ps[0]",
            "\t\t#define pr tr[p].ps[1]",
            "\t\t#define pf tr[p].fa",
            "\t\tint Q[N];",
            "\t\tstruct QAQ{int fa,ans,add,ps[2];}tr[N];",
            "\t\tinline void add(Re p,Re v){tr[p].ans+=v,tr[p].add+=v;}",
            "\t\tinline void pushdown(Re p){",
            "\t\t\tRe v=tr[p].add;",
            "\t\t\tif(v){",
            "\t\t\t\tif(pl)add(pl,v);",
            "\t\t\t\tif(pr)add(pr,v);",
            "\t\t\t\ttr[p].add=0;",
            "\t\t\t}",
            "\t\t}",
            "\t\tinline int nort(Re p){return tr[pf].ps[0]==p||tr[pf].ps[1]==p;}",
            "\t\tinline int which(Re p){return tr[pf].ps[1]==p;}",
            "\t\tinline void connect(Re p,Re fa,Re o){tr[pf=fa].ps[o]=p;}",
            "\t\tinline void rotate(Re p){",
            "\t\t\tRe fa=pf,fas=which(p);",
            "\t\t\tRe pa=tr[fa].fa,pas=which(fa);",
            "\t\t\tRe x=tr[p].ps[fas^1];",
            "\t\t\tif(nort(fa))tr[pa].ps[pas]=p;pf=pa;",
            "\t\t\tconnect(x,fa,fas),connect(fa,p,fas^1);",
            "\t\t}",
            "\t\tinline void splay(Re p){",
            "\t\t\tRe x=p,t=0;Q[++t]=x;",
            "\t\t\twhile(nort(x))Q[++t]=x=tr[x].fa;",
            "\t\t\twhile(t)pushdown(Q[t--]);",
            "\t\t\tfor(Re fa;nort(p);rotate(p))",
            "\t\t\t\tif(nort(fa=pf))rotate(which(p)==which(fa)?fa:p);",
            "\t\t}",
            "\t\tinline void access(Re p){for(Re son=0;p;son=p,p=pf)splay(p),pr=son;}",
            "\t\tinline void link(Re x,Re y){tr[x].fa=y,access(y),splay(y),add(y,tr[x].ans);}",
            "\t\tinline void cut(Re x){",
            "\t\t\taccess(x),splay(x),add(tr[x].ps[0],-tr[x].ans);",
            "\t\t\ttr[tr[x].ps[0]].fa=0,tr[x].ps[0]=0;",
            "\t\t}",
            "\t\tinline int ask(Re x){splay(x);return tr[x].ans;}",
            "\t}LCT;",
            "\tint O,last,link[N],maxlen[N],trans[N][30];",
            "\tSuffix_Automaton(){last=O=1;}",
            "\tinline void insert(char ch){",
            "\t\tRe z=++O,p=last;LCT.tr[z].ans=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,LCT.link(z,1);",
            "\t\telse{",
            "\t\t\tRe x=trans[p][ch];",
            "\t\t\tif(maxlen[x]==maxlen[p]+1)link[z]=x,LCT.link(z,x);",
            "\t\t\telse{",
            "\t\t\t\tRe y=++O;maxlen[y]=maxlen[p]+1;",
            "\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],LCT.cut(x),LCT.link(y,link[y]);",
            "\t\t\t\tlink[x]=link[z]=y,LCT.link(x,y),LCT.link(z,y);",
            "\t\t\t}",
            "\t\t}",
            "\t\tlast=z;",
            "\t}",
            "\tinline void add(string ch){",
            "\t\tfor(Re i=0;ch[i];++i)insert(ch[i]-'A');",
            "\t}",
            "\tinline int ask(string ch){",
            "\t\tRe p=1;",
            "\t\tfor(Re i=0;ch[i];++i)if(!(p=trans[p][ch[i]-'A']))return 0;",
            "\t\treturn LCT.ask(p);",
            "\t}",
            "}SAM;",
            "int main(){",
            "//    freopen(\"123.txt\",\"r\",stdin);",
            "    in(T),scanf(\"%s\",s+1);",
            "    for(Re i=1;s[i];++i)SAM.insert(s[i]-'A');",
            "\twhile(T--){",
            "\t\tscanf(\"%s\",op);string s=In(mask);",
            "\t\tif(op[0]=='A')SAM.add(s);",
            "\t\telse mask^=(ans=SAM.ask(s)),printf(\"%d\\n\",ans);",
            "\t}",
            "}",
            ""
        ],
        "description": "插入 LCT动态维护SAM"
    }
}