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. every row of W is a classifier for one of the classes

    every row of W is a classifier for one of the classes As we saw above, every row of W is a classifie ...

  2. [转载] 把Nutch爬虫部署到Hadoop集群上

    http://f.dataguru.cn/thread-240156-1-1.html 软件版本:Nutch 1.7, Hadoop 1.2.1, CentOS 6.5, JDK 1.7 前面的3篇文 ...

  3. js:深入函数的定义

    函数定义方式: 1.function fun1(){alert("fun1");}  //函数就是一个很特殊的对象.是一个Function的实例.事实上在内存中存储的操作是通过一个 ...

  4. Android Studio实现代码混淆

     1,在build.grandle添加,其中规则写在proguard-rules.pro中,也可以自定义一个文件,将其代替,比如eclipse常用的 proguard-project.txt: bui ...

  5. python基础10 ---匿名函数和递归

    一.匿名函数 1.lambda表达式就相当于匿名函数,其格式为: lambda 参数列表:参数表达式 2.lambda自带return值,因为匿名函数有个限制,就是只能有一个表达式,不用写return ...

  6. oldboyshell编程扩展内容

    oldboyshell编程扩展内容一.命令的优先级 命令分为: ==> alias ==> Compound Commands ==> function  ==> build_ ...

  7. 1django 视图与网址

    创建一个项目,名字叫mysite django-admin startproject mysite(项目名) 成功后,看到如下样式 mysite ├── manage.py └── mysite ├─ ...

  8. Java基础教程:反射基础

    Java基础教程:反射基础 引入反射 反射是什么 能够动态分析类能力的程序称为反射. 反射是一种很强大且复杂的机制. Class类 在程序运行期间,Java运行时系统始终为所有对象维护一个被称为运行时 ...

  9. eclipse中,项目无法在tomcat中发布(project facet java version 1.7 is not supported)

    在tomcat中发布项目时无法添加项目,错误信息:project facet java version 1.7 is not supported,如下图 这是由于你的tomcat的jdk版本低于你项目 ...

  10. ubuntu14.04 安装pip vitualenv flask

    安装pip: $ apt-get install python-pip$ pip -V #查看版本 确认安装成功 安装完pip后,会发现下载的速度特别慢.按如下修改: $ vim ~/.pip/pip ...