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

{
    "Cyclical Quest": {
        "prefix": "acm-6.6暴力解法-广义SAM不加特判会MLE",
        "body": [
            "#include<algorithm>",
            "#include<cstring>",
            "#include<cstdio>",
            "#include<vector>",
            "#include<map>",
            "#define LL long long",
            "#define Re register int",
            "using namespace std;",
            "const int N=3e6+5,M=1e6+3,logN=21;",
            "int n,T,len[M],vis[N<<1],tmp[N<<1];LL ans;char s[M];",
            "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;",
            "}",
            "vector<int>Q[M];",
            "struct Suffix_Automaton{",
            "\tint O,last,siz[N<<1],link[N<<1],maxlen[N<<1];map<int,int>trans[N<<1];",
            "\tSuffix_Automaton(){O=1;}",
            "\tinline int insert(Re ch,Re op,Re last){",
            "\t\tif(trans[last].find(ch)!=trans[last].end()){",
            "\t\t\tRe p=last,x=trans[p][ch];",
            "            if(maxlen[p]+1==maxlen[x])return x;",
            "            else{",
            "                Re y=++O;maxlen[y]=maxlen[p]+1;",
            "                for(map<int,int>::iterator it=trans[x].begin();it!=trans[x].end();++it)trans[y].insert(*it);",
            "                while(p&&trans[p][ch]==x)trans[p][ch]=y,p=link[p];",
            "                link[y]=link[x],link[x]=y;",
            "                return y;",
            "            }",
            "\t\t}",
            "\t\tRe z=++O,p=last;if(op)siz[z]=1;maxlen[z]=maxlen[p]+1;",
            "\t\twhile(p&&trans[p].find(ch)==trans[p].end())trans[p][ch]=z,p=link[p];",
            "\t\tif(!p)link[z]=1;",
            "\t\telse{",
            "\t\t\tRe x=trans[p][ch];",
            "\t\t\tif(maxlen[x]==maxlen[p]+1)link[z]=x;",
            "\t\t\telse{",
            "\t\t\t\tRe y=++O;maxlen[y]=maxlen[p]+1;",
            "\t\t\t\tfor(map<int,int>::iterator it=trans[x].begin();it!=trans[x].end();++it)trans[y].insert(*it);",
            "\t\t\t\twhile(p&&trans[p][ch]==x)trans[p][ch]=y,p=link[p];",
            "\t\t\t\tlink[y]=link[x],link[x]=link[z]=y;",
            "\t\t\t}",
            "\t\t}",
            "\t\treturn z;",
            "\t}",
            "\tint o,head[N<<1];",
            "\tstruct QAQ{int to,next;}a[N<<1];",
            "\tinline void add(Re x,Re y){a[++o].to=y,a[o].next=head[x],head[x]=o;}",
            "\tinline void dfs(Re x){",
            "\t\tfor(Re i=head[x];i;i=a[i].next)dfs(a[i].to),siz[x]+=siz[a[i].to];",
            "\t}",
            "\tinline void build(){",
            "\t\tfor(Re i=2;i<=O;++i)add(link[i],i);dfs(1);",
            "\t}",
            "\tinline int ask(Re p,Re len){",
            "\t\twhile(maxlen[link[p]]>=len)p=link[p];return p;",
            "\t}",
            "}SAM;",
            "int main(){",
            "//    freopen(\"123.txt\",\"r\",stdin);",
            "    scanf(\"%s\",s+1),n=strlen(s+1),in(T);",
            "    for(Re i=1,last=1;s[i];++i)last=SAM.insert(s[i]-'a',1,last);",
            "    for(Re i=1;i<=T;++i){",
            "    \tscanf(\"%s\",s+1),len[i]=strlen(s+1);Re last=1;",
            "    \tfor(Re j=1;j<len[i];++j)last=SAM.insert(s[j]-'a',0,last);",
            "    \tQ[i].push_back(last=SAM.insert(s[len[i]]-'a',0,last));",
            "    \tfor(Re j=1;j<len[i];++j)Q[i].push_back(last=SAM.insert(s[j]-'a',0,last));",
            "\t}",
            "\tSAM.build();",
            "    for(Re i=1;i<=T;++i){",
            "    \ttmp[0]=ans=0;",
            "    \tfor(Re j=0,SZ=Q[i].size();j<SZ;++j){",
            "    \t\tRe x=SAM.ask(Q[i][j],len[i]);",
            "    \t\tif(vis[x])continue;",
            "    \t\ttmp[++tmp[0]]=x,vis[x]=1,ans+=SAM.siz[x];",
            "\t\t}",
            "\t\tfor(Re j=1;j<=tmp[0];++j)vis[tmp[j]]=0;",
            "\t\tprintf(\"%lld\\n\",ans);",
            "\t}",
            "}",
            ""
        ],
        "description": "插入 Cyclical Quest"
    }
}