Oimaster and sevenk love each other.

    But recently,sevenk heard that a girl named ChuYuXun was dating with oimaster.As a woman's nature, sevenk felt angry and began to check oimaster's online talk with ChuYuXun.    Oimaster talked with ChuYuXun 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?"
 
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

题意:

给定一些模板串,询问每个匹配串在多少个模板串里出现过。

思路:

后缀数组办法:

全部模板串连接在一起SA排序,SA数组附件的串里面去验证原串,还没写,我说不清楚,大概是后缀数组+RMQ处理。

后缀自动机办法:

1,广义后缀自动机+dfs序列+。。。

2,后缀自动机+标记,对于每个新的后缀集合np,标记它最近出现的原串,对每个模板串出现的新串的数量++。(目前发现最快的版本,如下)

经验:

  • 与普通的后缀自动机不同的是,广义后缀自自动机在加入一个新串的时候,从root开始。
  • 为了记录每个状态在哪些串出现过,会在每个np向上传递,我的代码是用的没加一个字符,向上传递一次。也可以用bitset记录在哪里出现过等到加完所有字符串后再拓扑排序,然后“亦或”向上传递。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#define N 200003
using namespace std;
int ch[N][],fa[N],l[N],n,m,len;
int r[N],v[N],cnt,np,p,nq,q,last,root,nxt[N],now,size[N];
char s[N];
void extend(int x)
{
int c=s[x]-'a';
p=last; np=++cnt; last=np;
l[np]=l[p]+;
for (;p&&!ch[p][c];p=fa[p]) ch[p][c]=np;
if (!p) fa[np]=root;
else {
q=ch[p][c];
if (l[q]==l[p]+) fa[np]=q;
else {
nq=++cnt; l[nq]=l[p]+;
memcpy(ch[nq],ch[q],sizeof ch[nq]); size[nq]=size[q]; nxt[nq]=nxt[q];
fa[nq]=fa[q];
fa[q]=fa[np]=nq;
for (;ch[p][c]==q;p=fa[p]) ch[p][c]=nq;
}
}
for (;np;np=fa[np])
if (nxt[np]!=now) {
size[np]++;
nxt[np]=now;
}
else break;
}
int main()
{
scanf("%d%d",&n,&m);
root=++cnt;
for(int i=;i<=n;i++) {
scanf("%s",s+);
last=root;
len=strlen(s+);
now=i;
for (int j=;j<=len;j++)
extend(j);
}
for (int i=;i<=m;i++) {
scanf("%s",s+);
len=strlen(s+);
p=root;
for (int j=;j<=len;j++) p=ch[p][s[j]-'a'];
printf("%d\n",size[p]);
}
}

SPOJ8093Sevenk Love Oimaster(广义后缀自动机)的更多相关文章

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

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

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

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

  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. BZOJ 2780: [Spoj]8093 Sevenk Love Oimaster [广义后缀自动机]

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

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

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

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

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

  8. 【BZOJ2780】Sevenk Love Oimaster【广义后缀自动机】

    题意 给出你n个字符串和q个查询,每个查询给出一个字符串s,对于每个查询你都要输出这个字符串s在上面多少个字符串中出现过. 分析 广义后缀自动机的裸题.建好SAM以后再跑一遍得到每个状态的ocu和la ...

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

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

随机推荐

  1. wordpress 获取分类ID,分类标题,分类描述,分类链接url函数

    get_cat_ID()    根据分类名称获取分类ID   ///// get_cat_name()    根据分类ID获取分类名称 用法:<?phpget_cat_ID( $cat_name ...

  2. maven;tomcat配置

    [说明]今天呀,上午刚刚打算写javaweb项目,服务器就出现了问题,就花了点时间搞定了:下午听老大说了任务的大致内容和意义,收获颇多:晚上去服务器上部署了maven,听说可以实现热部署 一:今天完成 ...

  3. 【BZOJ4128】Matrix BSGS+hash

    [BZOJ4128]Matrix Description 给定矩阵A,B和模数p,求最小的x满足 A^x = B (mod p) Input 第一行两个整数n和p,表示矩阵的阶和模数,接下来一个n * ...

  4. EasyDSS流媒体解决方案实现的实时数据统计报表、视频文件上传、点播、分享、集成代码等功能

    之前的EasyDSS作为rtmp流媒体服务器自从推出就备受用户好评,随着用户的需求的变更产品自身的发展是必须的: 为了更好的用户体验和和功能的完善,我们在EasyDSS的基础上增添了服务器硬件数据报表 ...

  5. Webpack探索【5】--- plugins详解

    本文主要讲plugins相关内容. https://gitbook.cn/gitchat/column/59e065f64f7fbe555e479204/topic/59e96d87a35cf44e1 ...

  6. HDU 4513 吉哥系列故事――完美队形II(Manacher)

    题目链接:cid=70325#problem/V">[kuangbin带你飞]专题十六 KMP & 扩展KMP & Manacher V - 吉哥系列故事――完美队形I ...

  7. ABAP制作密码输入框

    [转自 http://blog.csdn.net/saphome/article/details/6956911] 这几天做一个系统维护的程序,需要用户输入用户名和密码登录.可怎样实现输入密码显示星号 ...

  8. MyBatis:学习笔记(4)——动态SQL

    MyBatis:学习笔记(4)——动态SQL

  9. 自动分割nginx服务的日志文件

    nginx服务每天都会产生大量的日志信息,时间一长导致日志文件容量很大,会影响系统性能.通过以下shell代码,配合crontab定时执行可实现nginx日志定时分割的功能. #!/bin/bash ...

  10. 第三章 python中的字符串

    一.字符串的基本操作 所有标准的序列操作对字符串同样适用,如索引.分片.乘法.判断成员是否存在.求长度.最大值和最小值等.记住一点,字符串是不可变的. 二.字符串中重要的方法 1.find(subst ...