Description

Oimaster and sevenk love each other.
But recently,sevenk heard that a girl named ChuYuXun was dating with oimaster.As a woman's nature, s
evenk felt angry and began to check oimaster's online talk with ChuYuXun.    Oimaster talked with Ch
uYuXun n times, and each online talk actually is a string.Sevenk asks q questions like this,    "how
 many strings in oimaster's online talk contain this string as their substrings?"
有n个大串和m个询问,每次给出一个字符串s询问在多少个大串中出现过

Input

There are two integers in the first line, 
the number of strings n and the number of questions q.
And n lines follow, each of them is a string describing oimaster's online talk. 
And q lines follow, each of them is a question.
n<=10000, q<=60000 
the total length of n strings<=100000, 
the total length of q question strings<=360000

Output

For each question, output the answer in one line.

Sample Input

3 3
abcabcabc
aaa
aafe
abc
a
ca

Sample Output

1
3
1

解题思路:

利用Parent树的子节点都是父节点母串的性质,在大串的每个实节点打上标记,然后构建出这颗Parent树。

那么我们的问题就变成了在一个Fail节点子树内不同颜色个数。

像不像HH的项链?

把Dfs序构建出来,离线树状数组就好了。

代码:

 #include<cstdio>
#include<cstring>
#include<algorithm>
const int N=;
const int M=;
struct sant{
int tranc[];
int len;
int pre;
}s[N];
struct pnt{
int hd;
int col;
int ind;
int oud;
}p[N];
struct ent{
int twd;
int lst;
}e[N];
struct qust{
int l,r;
int ans;
int no;
}q[N];
int siz;
int fin;
int n,Q;
int cnt;
int dfn;
char tmp[N];
int line[N];
int lst[N];
int phr[N];
int col[N];
void Insert(int c,int whic)
{
int nwp,nwq,lsp,lsq;
nwp=++siz;
p[nwp].col=whic;
s[nwp].len=s[fin].len+;
for(lsp=fin;lsp&&!s[lsp].tranc[c];lsp=s[lsp].pre)
s[lsp].tranc[c]=nwp;
if(!lsp)
s[nwp].pre=;
else{
lsq=s[lsp].tranc[c];
if(s[lsq].len==s[lsp].len+)
s[nwp].pre=lsq;
else{
nwq=++siz;
s[nwq]=s[lsq];
s[nwq].len=s[lsp].len+;
s[nwp].pre=s[lsq].pre=nwq;
while(s[lsp].tranc[c]==lsq)
{
s[lsp].tranc[c]=nwq;
lsp=s[lsp].pre;
}
}
}
fin=nwp;
return ;
}
void ade(int f,int t)
{
cnt++;
e[cnt].twd=t;
e[cnt].lst=p[f].hd;
p[f].hd=cnt;
return ;
}
void Dfs(int x)
{
p[x].ind=++dfn;
phr[dfn]=x;
col[dfn]=p[x].col;
for(int i=p[x].hd;i;i=e[i].lst)
{
int to=e[i].twd;
Dfs(to);
}
p[x].oud=dfn;
return ;
}
bool cmp(qust x,qust y)
{
return x.r<y.r;
}
bool cmq(qust x,qust y)
{
return x.no<y.no;
}
int lowbit(int x)
{
return x&(-x);
}
void update(int pos,int x)
{
while(pos&&pos<=siz)
{
line[pos]+=x;
pos+=lowbit(pos);
}
return ;
}
int query(int pos)
{
int ans=;
while(pos)
{
ans+=line[pos];
pos-=lowbit(pos);
}
return ans;
}
int main()
{
fin=++siz;
scanf("%d%d",&n,&Q);
for(int i=;i<=n;i++)
{
scanf("%s",tmp+);
fin=;
int len=strlen(tmp+);
for(int j=;j<=len;j++)
Insert(tmp[j]-'a',i);
}
for(int i=;i<=siz;i++)
ade(s[i].pre,i);
Dfs(); for(int i=;i<=Q;i++)
{
scanf("%s",tmp+);
int root=;
int len=strlen(tmp+);
for(int j=;j<=len;j++)
root=s[root].tranc[tmp[j]-'a'];
q[i].no=i;
if(!root)
continue;
q[i].l=p[root].ind;
q[i].r=p[root].oud;
}
std::sort(q+,q+Q+,cmp);
int rr=;
for(int i=;i<=Q;i++)
{
while(rr<=q[i].r)
{
if(!col[rr])
{
rr++;
continue;
}
if(lst[col[rr]])
update(lst[col[rr]],-);
update(rr,);
lst[col[rr]]=rr;
rr++;
}
rr--;
if(!q[i].l)
continue;
q[i].ans=query(q[i].r)-query(q[i].l-);
}
std::sort(q+,q+Q+,cmq);
for(int i=;i<=Q;i++)
printf("%d\n",q[i].ans);
return ;
}

BZOJ2780: [Spoj]8093 Sevenk Love Oimaster(广义后缀自动机,Parent树,Dfs序)的更多相关文章

  1. 【BZOJ2780】[Spoj]8093 Sevenk Love Oimaster 广义后缀自动机

    [BZOJ2780][Spoj]8093 Sevenk Love Oimaster Description Oimaster and sevenk love each other.     But r ...

  2. BZOJ 2780: [Spoj]8093 Sevenk Love Oimaster [广义后缀自动机]

    JZPGYZ - Sevenk Love Oimaster     Oimaster and sevenk love each other.       But recently,sevenk hea ...

  3. bzoj 3277 串 && bzoj 3473 字符串 && bzoj 2780 [Spoj]8093 Sevenk Love Oimaster——广义后缀自动机

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3277 https://www.lydsy.com/JudgeOnline/problem.p ...

  4. BZOJ 2780 [Spoj]8093 Sevenk Love Oimaster ——广义后缀自动机

    给定n个串m个询问,问每个串在n个串多少个串中出现了. 构建广义后缀自动机,(就是把所有字符串的后缀自动机合并起来)其实只需要add的时候注意一下就可以了. 然后对于每一个串,跑一边匹配,到达了now ...

  5. 【BZOJ2780】【SPOJ】Sevenk Love Oimaster(后缀自动机)

    [BZOJ2780][SPOJ]Sevenk Love Oimaster(后缀自动机) 题面 BZOJ 洛谷 题解 裸的广义后缀自动机??? 建立广义后缀自动机建立出来之后算一下每个节点被几个串给包括 ...

  6. BZOJ2780 [Spoj]8093 Sevenk Love Oimaster 【广义后缀自动机】

    题目 Oimaster and sevenk love each other. But recently,sevenk heard that a girl named ChuYuXun was dat ...

  7. BZOJ.2780.[SPOJ8093]Sevenk Love Oimaster(广义后缀自动机)

    题目链接 \(Description\) 给定n个模式串,多次询问一个串在多少个模式串中出现过.(字符集为26个小写字母) \(Solution\) 对每个询问串进行匹配最终会达到一个节点,我们需要得 ...

  8. SP8093 JZPGYZ - Sevenk Love Oimaster(广义后缀自动机)

    题意 题目链接 Sol 广义后缀自动机板子题..和BZOJ串那个题很像 首先建出询问串的SAM,然后统计一下每个节点被多少个串包含 最后直接拿询问串上去跑就行了 #include<bits/st ...

  9. BZOJ2780——[Spoj]8093 Sevenk Love Oimaster

    0.题意:给定N个原始字符串S,M次查询某个特殊的字符串S'在多少个原始串中出现过. 1.分析:这个题我们第一感觉就是可以用后缀自动机来搞,然后我们发现不是本质不同的字串..求出现过的次数,也就是说多 ...

随机推荐

  1. linux中的硬连接和软连接

    linux中的硬连接和软连接 linux中的硬连接和软连接 背景 连接 硬连接 软连接 example reference 背景 linux中的文件主要分3块, - 真正的数据 - 索引节点号(ino ...

  2. 积跬步,聚小流------Bootstrap学习记录(2)

    现阶段开启每一次新的征程,已然离不开"Hello World"的习惯仪式.这次自然也不例外.先来看下给出的官网给出的演示样例: 1.bootstrap官网提供的html基本模板代码 ...

  3. hadoop集群中动态添加新的DataNode节点

    集群中现有的计算能力不足,须要另外加入新的节点时,使用例如以下方法就能动态添加新的节点: 1.在新的节点上安装hadoop程序,一定要控制好版本号,能够从集群上其它机器cp一份改动也行 2.把name ...

  4. POJ 1611 The Suspects 并查集 Union Find

    本题也是个标准的并查集题解. 操作完并查集之后,就是要找和0节点在同一个集合的元素有多少. 注意这个操作,须要先找到0的父母节点.然后查找有多少个节点的额父母节点和0的父母节点同样. 这个时候须要对每 ...

  5. 有关error PRJ0003错误的思考

    作者:朱金灿 来源:http://blog.csdn.net/clever101 今天同事遇到两个编译错误: 项目: error PRJ0003 : 生成"rc.exe"时出错. ...

  6. Python学习笔记 capture 1

    最近开始学习Python3.x,真的感觉Python的语法与C++,Java有很大的不同,Python从某些方面来说语法更简单.Python作为一种解释性语言和编译型语言如C++来说,还是各有千秋的. ...

  7. js获取当前时间年份,处理年月日

    js中获得当前时间年份.月份.日期       //获取完整的日期 var date=new Date; var y = date.getFullYear()var m = date.getMonth ...

  8. python垃圾回收算法

    标准python垃圾回收器由两部分组成,即引用计数回收器和分代垃圾回收器(即python包中的gc module).其中,引用计数模块不能被禁用,而GC模块可以被禁用. 引用计数算法 python中每 ...

  9. HDU 1668 Islands and Bridges

    Islands and Bridges Time Limit: 4000ms Memory Limit: 65536KB This problem will be judged on HDU. Ori ...

  10. AFNetworking 取消请求

    取消单个操作: AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request] ...