What-The-Fatherland is a strange country! All phone numbers there are strings consisting of lowercase English letters. What is double strange that a phone number can be associated with several bears!

In that country there is a rock band called CF consisting of n bears (including Mike) numbered from 1 to n.

Phone number of i-th member of CF is si. May 17th is a holiday named Phone Calls day. In the last Phone Calls day, everyone called all the numbers that are substrings of his/her number (one may call some number several times). In particular, everyone called himself (that was really strange country).

Denote as call(i, j) the number of times that i-th member of CF called the j-th member of CF.

The geek Mike has q questions that he wants to ask you. In each question he gives you numbers l, r and k and you should tell him the number

Input

The first line of input contains integers n and q (1 ≤ n ≤ 2 × 105 and 1 ≤ q ≤ 5 × 105).

The next n lines contain the phone numbers, i-th line contains a string si consisting of lowercase English letters ().

The next q lines contain the information about the questions, each of them contains integers l, r and k (1 ≤ l ≤ r ≤ n and 1 ≤ k ≤ n).

Output

Print the answer for each question in a separate line.

Examples

Input
5 5
a
ab
abab
ababab
b
1 5 1
3 5 1
1 5 2
1 5 3
1 4 5
Output
7
5
6
3
6

题意:题目给你N个字符串,Q组询问(1<=N , Q<=200000).(∑s[i] <=200000 )
每组询问,l, r,x  : 在第 l个 字符串  到 第r个字符串这(r-l+1)个字符串中,第x个字符串出现的次数。


题解一(后缀自动机):

我们将这N个字符串建立SAM,然后将其parent tree拉出来,那么对于一个节点所表示的单词,他出现的次数就是他的子树的大小,由于题目是要求l~r之间,所以是他的子树中endpos在一定范围内的点的数量。我们建立SAM时,对于第k个字符串,每加入一个节点,就在对应节点的线段树里面的第k个位置加一。 我们用线段树维护每个节点的子树,然后线段树合并。

对于每一个询问,我们只要在对应的子树里面找l~r区间的和就行了。

参考代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pii pair<int,int>
#define pil pair<int,ll>
#define mkp make_apir
const int INF=0x3f3f3f3f;
const int maxn=2e5+;
int N,Q;
char s[maxn]; int nxt[maxn<<][],l[maxn<<],fa[maxn<<];
int last,tot,cnt[maxn<<],c[maxn<<];
int sz,p[maxn],T[maxn<<]; struct Tr{
int ls,rs;
int num;
} tr[maxn*]; void Update(int &x,int l,int r,int pos)
{
if(!x) x=++sz;
tr[x].num++;
if(l==r) return ;
int mid=l+r>>;
if(pos<=mid) Update(tr[x].ls,l,mid,pos);
else Update(tr[x].rs,mid+,r,pos);
} int Query(int x,int l,int r,int L,int R)
{
if(!x) return ;
if(L<=l&&r<=R) return tr[x].num;
int mid=l+r>>,res=;
if(L<=mid) res+=Query(tr[x].ls,l,mid,L,R);
if(R>mid) res+=Query(tr[x].rs,mid+,r,L,R);
return res;
} int Merge(int x,int y)
{
if(!x||!y) return x+y;
int z=++sz;
tr[z].ls=Merge(tr[x].ls,tr[y].ls);
tr[z].rs=Merge(tr[x].rs,tr[y].rs);
tr[z].num=tr[x].num+tr[y].num;
return z;
} void Mer()
{
for(int i=;i<=tot;++i) cnt[l[i]]++;
for(int i=;i<=tot;++i) cnt[i]+=cnt[i-];
for(int i=;i<=tot;++i) c[cnt[l[i]]--]=i;
for(int i=tot,x;i>;--i) x=c[i],T[fa[x]]=Merge(T[x],T[fa[x]]);
} void Init()
{
last=tot=; sz=;
memset(nxt[tot],,sizeof nxt[tot]);
l[tot]=fa[tot]=;
} int NewNode()
{
++tot;
memset(nxt[tot],,sizeof nxt[tot]);
l[tot]=fa[tot]=;
return tot;
} void Insert(int ch,int x)
{
int p,q,np,nq;
if(nxt[last][ch])
{
p=last;q=nxt[p][ch];
if(l[q]==l[p]+) last=q;
else
{
nq=NewNode();
l[nq]=l[p]+;
memcpy(nxt[nq],nxt[q],sizeof(nxt[q]));
fa[nq]=fa[q];fa[q]=nq;
while(p&&nxt[p][ch]==q) nxt[p][ch]=nq,p=fa[p];
last=nq;
}
}
else
{
np=NewNode(),p=last;
last=np; l[np]=l[p]+;
while(p&&!nxt[p][ch]) nxt[p][ch]=np,p=fa[p];
if(!p) fa[np]=;
else
{
q=nxt[p][ch];
if(l[q]==l[p]+) fa[np]=q;
else
{
nq=NewNode();
memcpy(nxt[nq],nxt[q],sizeof nxt[q]);
fa[nq]=fa[q];
l[nq]=l[p]+;
fa[q]=fa[np]=nq;
while(p&&nxt[p][ch]==q) nxt[p][ch]=nq,p=fa[p];
}
}
}
} int main()
{
scanf("%d%d",&N,&Q);
Init();
for(int i=;i<=N;++i)
{
scanf("%s",s);
int len=strlen(s); last=;
for(int j=;j<len;++j) Insert(s[j]-'a',i),Update(T[last],,N,i);
p[i]=last;
}
Mer(); while(Q--)
{
int l,r,x;
scanf("%d%d%d",&l,&r,&x);
printf("%d\n",Query(T[p[x]],,N,l,r));
} return ;
}

题解二(AC自动机):首先对于多串匹配问题,我们很容易想到AC自动机。在AC自动机的fail树中的每个节点,我们知道他出现的次数就是他的子树的大小。但是这题是要询问在第 l个 字符串  到 第r个字符串这(r-l+1)个字符串中,第x个字符串出现的次数,这我们可以想到主席树的询问历史版本信息的性质,那么我们就可以用主席树去维护AC自动机的fail指针的dfs序。然后我们只要求出第R棵树和地(L-1)棵树的差值就行了。

参考代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pii pair<int,int>
#define pil pair<int,ll>
#define mkp make_apir
const int INF=0x3f3f3f3f;
const int maxn=2e5+;
int N,Q;
char s[maxn];
int sz,ch[maxn][],fail[maxn],val[maxn];
int in[maxn],out[maxn],tim,Len[maxn],tot,T[maxn];
vector<int> vec[maxn],ts[maxn];
void Init()
{
sz=; tim=tot=;
memset(ch[sz],,sizeof ch[sz]);
memset(val,,sizeof val);
}
int idx(char c){return c-'a';}
void Insert(char *s,int x)
{
Len[x]=strlen(s);int u=;
for(int i=;i<Len[x];++i)
{
int c=idx(s[i]); ts[x].push_back(c);
if(!ch[u][c]){memset(ch[sz],,sizeof ch[sz]);ch[u][c]=sz++;}
u=ch[u][c];
}
val[x]=u;
}
void GetFail()
{
queue<int> q;
fail[]=;
for(int i=;i<;++i)
if(ch[][i]) {fail[ch[][i]]=;q.push(ch[][i]);} while(!q.empty())
{
int u=q.front();q.pop();
vec[fail[u]].push_back(u);
for(int i=;i<;++i)
{
int c=ch[u][i];
if(!c){ch[u][i]=ch[fail[u]][i];continue;}
q.push(c);
fail[c]=ch[fail[u]][i];
}
}
}
void dfs(int u)
{
in[u]=++tim;
for(int i=,len=vec[u].size();i<len;++i)
dfs(vec[u][i]);
out[u]=tim;
} struct Tree{
int ls,rs;
int num;
} tr[maxn*]; inline void Update(int y,int &x,int l,int r,int pos)
{
tr[++tot]=tr[y];tr[tot].num++; x=tot;
if(l==r) return;
int mid=l+r>>;
if(pos<=mid) Update(tr[y].ls,tr[x].ls,l,mid,pos);
else Update(tr[y].rs,tr[x].rs,mid+,r,pos);
}
inline void Build(int y,int &x,int id)
{
int now=,last=y;
for(int i=;i<Len[id];++i)
{
now=ch[now][ts[id][i]];
Update(last,x,,tim,in[now]);
last=x;
}
}
inline int Query(int y,int x,int l,int r,int L,int R)
{
if(L<=l&&r<=R) return tr[x].num-tr[y].num;
int mid=l+r>>,res=;
if(L<=mid) res+=Query(tr[y].ls,tr[x].ls,l,mid,L,R);
if(R>mid) res+=Query(tr[y].rs,tr[x].rs,mid+,r,L,R);
return res;
} int main()
{
scanf("%d%d",&N,&Q);
Init();
for(int i=;i<=N;++i) scanf("%s",s),Insert(s,i);
GetFail();
dfs();
for(int i=;i<=N;++i) Build(T[i-],T[i],i);
while(Q--)
{
int l,r,x;
scanf("%d%d%d",&l,&r,&x);
printf("%d\n",Query(T[l-],T[r],,tim,in[val[x]],out[val[x]]));
} return ;
}

CF547E Milk and Friends(AC自动机的fail指针上建主席树 或 广义后缀自动机的parent线段树合并)的更多相关文章

  1. BZOJ 3926: [Zjoi2015]诸神眷顾的幻想乡 广义后缀自动机 后缀自动机 字符串

    https://www.lydsy.com/JudgeOnline/problem.php?id=3926 广义后缀自动机是一种可以处理好多字符串的一种数据结构(不像后缀自动机只有处理一到两种的时候比 ...

  2. [bzoj3277==bzoj3473]出现k次子串计数——广义后缀自动机+STL

    Brief Description 给定n个字符串,对于每个字符串,您需要求出在所有字符串中出现次数大于等于k次的子串个数. Algorithm Design 先建立一个广义后缀自动机,什么是广义后缀 ...

  3. AC自动机 - 关于Fail指针

    fail指针可以说是AC自动机里最难理解的东西,怎样更好的理解AC自动机的fail指针? 先来看一幅图: 看这幅图上的fail指针是怎么构造的. 树上的词分别是: { he , hers , his ...

  4. UVA - 11107 Life Forms (广义后缀自动机+后缀树/后缀数组+尺取)

    题意:给你n个字符串,求出在超过一半的字符串中出现的所有子串中最长的子串,按字典序输出. 这道题算是我的一个黑历史了吧,以前我的做法是对这n个字符串建广义后缀自动机,然后在自动机上dfs,交上去AC了 ...

  5. 后缀自动机(SAM)+广义后缀自动机(GSA)

    经过一顿操作之后竟然疑似没退役0 0 你是XCPC选手吗?我觉得我是! 稍微补一点之前丢给队友的知识吧,除了数论以外都可以看看,为Dhaka和新队伍做点准备... 不错的零基础教程见 IO WIKI ...

  6. BZOJ 3277 串 (广义后缀自动机)

    3277: 串 Time Limit: 10 Sec Memory Limit: 128 MB Submit: 309 Solved: 118 [Submit][Status][Discuss] De ...

  7. UVA - 11107 Life Forms (广义后缀自动机)

    题意:给你n个字符串,求出在超过一半的字符串中出现的所有子串中最长的子串,按字典序输出. 对这n个字符串建广义后缀自动机,建完后每个字符串在自动机上跑一遍,沿fail树向上更新所有子串结点的出现次数( ...

  8. [Bzoj4566][Haoi2016]找相同字符(广义后缀自动机)

    4566: [Haoi2016]找相同字符 Time Limit: 20 Sec  Memory Limit: 256 MBSubmit: 861  Solved: 495[Submit][Statu ...

  9. 【BZOJ2806】【CTSC2012】Cheat 广义后缀自动机+二分+Dp

    题目 题目在这里 思路&做法 我们先对标准作文库建广义后缀自动机. 然后对于每一篇阿米巴的作文, 我们首先把放到广义后缀自动机跑一遍, 对于每一个位置, 记录公共子串的长度\((\)即代码和下 ...

随机推荐

  1. 《计算机网络 自顶向下方法》 第8章 计算机网络中的安全 Part2

    SSL(使 TCP 连接安全) SSL(Secure Socket Layer),即安全套接字层,是对 TCP 的强化 HTTPS 使用 SSL,而 HTTP 不使用 SSL 通过采用机密性.数据完整 ...

  2. T-SQL Part X: UNION, EXCEPT and INTERSECT

    MSDN上关于EXCEPT和INTERSECT的文档.MSDN上关于UNION的文档. 值得注意的是,UNION其实有两种,一种是普通的UNION,另外一种是UNION ALL.加上EXCEPT和IN ...

  3. C++中对C的扩展学习新增语法——引用

    引用 引用的好处: 1.引用的好处 C++使用结构体,不需要再使用 typedef. 2.值传递是将实参进行拷贝,赋值给形参,如果对象比较大,每次拷贝效率比较低,并且函数内部无法修改外部变量的值,能力 ...

  4. 使用Topshelf组件 一步一步创建 Windows 服务

    我们先来介绍一下使用它的好处,以下论述参考自其他大神. topshelf是创建windows服务的一种方式,相比原生实现ServiceBase.Install.Installer更为简单方便, 我们只 ...

  5. 利用SSH隧道技术穿越内网访问远程设备

    本文为作者原创,转载请注明出处:https://www.cnblogs.com/leisure_chn/p/11899478.html 通常,我们用于调试的计算机无法远程访问位于局域网中的待调试设备. ...

  6. thinkphp 获取前端传递过来的参数

    thinkphp 获取前端传递过来的参数 use think\facade\Request; // 获取当前请求的name变量 Request::param('name'); // 获取当前请求的所有 ...

  7. vscode加入到鼠标右键

    新建.reg的文件,复制下面代码,然后运行 D:\\软件\\VsCode\\Microsoft VS Code\\Code.exe路径改为自己的,必须是两个 \\ 才能生效 Windows Regis ...

  8. Java程序性能优化之性能概述

    性能的基本概念 一).什么叫程序的性能? 程序运行所需的内存和时间. 二).性能的表现形式: 1).执行速度: 程序的反应是否迅速,响应时间是否足够短. 2).启动时间:程序从运行到可以处理正常业务所 ...

  9. d3.js 地铁轨道交通项目实战

    上一章说了如何制作一个线路图,当然上一章是手写的JSON数据,当然手写的json数据有非常多的好处,例如可以应对客户的各种BT需求,但是大多数情况下我们都是使用地铁公司现成的JSON文件,话不多说我们 ...

  10. 作业要求20191010-8 alpha week 1/2 Scrum立会报告+燃尽图 06

    此作业要求参见:https://edu.cnblogs.com/campus/nenu/2019fall/homework/8751 一.小组情况 队名:扛把子 组长:迟俊文 组员:宋晓丽 梁梦瑶 韩 ...