题目

 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?"

输入格式

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

输出格式

For each question, output the answer in one line.

输入样例

3 3

abcabcabc

aaa

aafe

abc

a

ca

输出样例

1

3

1

题解

广义后缀自动机裸题

先建机,然后记录每个点所被包含的字符串的个数

询问就按询问的字符串走到对应节点,输出该节点存在的字符串数

#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<cstring>
#include<algorithm>
#define LL long long int
#define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define BUG(s,n) for (int i = 1; i <= (n); i++) cout<<s[i]<<' '; puts("");
using namespace std;
const int maxn = 200005,maxm = 400005,INF = 1000000000;
inline int read(){
int out = 0,flag = 1; char c = getchar();
while (c < 48 || c > 57){if (c == '-') flag = -1; c = getchar();}
while (c >= 48 && c <= 57){out = (out << 3) + (out << 1) + c - 48; c = getchar();}
return out * flag;
}
int n;
int ch[maxn][26],pre[maxn],step[maxn],cnt,last;
int sz[maxn],vis[maxn];
char ss[maxn >> 1];
string s[10005];
void ins(int x){
int p = last,np = ++cnt; step[np] = step[p] + 1; last = np;
while (p && !ch[p][x]) ch[p][x] = np,p = pre[p];
if (!p) pre[np] = 1;
else {
int q = ch[p][x];
if (step[q] == step[p] + 1) pre[np] = q;
else {
int nq = ++cnt; step[nq] = step[p] + 1;
for (int i = 0; i < 26; i++) ch[nq][i] = ch[q][i];
pre[nq] = pre[q]; pre[np] = pre[q] = nq;
while (ch[p][x] == q) ch[p][x] = nq,p = pre[p];
}
}
}
int main(){
n = read();
int q = read();
cnt = last = 1;
for (int i = 1; i <= n; i++){
last = 1;
scanf("%s",ss); int len = strlen(ss);
s[i] = (string)(ss);
for (int i = 0; i < len; i++) ins(ss[i] - 'a');
}
int u;
for (int i = 1; i <= n; i++){
u = 1;
for (unsigned int j = 0; j < s[i].length(); j++){
u = ch[u][s[i][j] - 'a'];
for (int p = u; p && vis[p] != i; p = pre[p])
sz[p]++,vis[p] = i;
}
}
while (q--){
scanf("%s",ss);
int len = strlen(ss),ans = 0;
u = 1;
for (int i = 0; i < len; i++){
if (!(u = ch[u][ss[i] - 'a'])) break;
if (i == len - 1) ans = sz[u];
}
printf("%d\n",ans);
}
return 0;
}

BZOJ2780 [Spoj]8093 Sevenk Love Oimaster 【广义后缀自动机】的更多相关文章

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

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

  2. 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 ...

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

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

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

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

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

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

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

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

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

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

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

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

  9. BZOJ2780: [Spoj]8093 Sevenk Love Oimaster(广义后缀自动机,Parent树,Dfs序)

    Description Oimaster and sevenk love each other. But recently,sevenk heard that a girl named ChuYuXu ...

随机推荐

  1. php类重载

    首先,因为PHP是弱类型语言,是不直接支持重载的,不过可以通过一些方法来实现重载. 先说一下重写 在子类继承父类方法后,自己重新定义父类的方法,包含函数名,形参(个数,顺序)完全相同,但权限修饰符可不 ...

  2. js 分组数组

    思路: 1.先将数组按照一定规则排序: 2.再拆分数组到Map中,按Key分类: 3.再从Map中遍历取出要显示的内容: sortBroadList: function (broadcastList) ...

  3. springmvc+maven搭建web项目

    1.创建一个maven project 为spring1 2.进行项目的配置:默认的java 1.5 在properties中选择project facts项目进行配置,反选web之后修改java环境 ...

  4. Ubuntu16.04下使用sublime text3搭建Python IDE

    本来是想用pycharm,但你看它的内存要求,我的虚拟机一共也就1G Vim太别扭了,就算有代码颜色,不能自动对齐,不能规范格式,跳转到函数定义,显示文档,要配置起来太费劲,所以就尝试着用sublim ...

  5. 导致实例逐出的五大问题 (文档 ID 1526186.1)

    适用于: Oracle Database - Enterprise Edition - 版本 10.2.0.1 到 11.2.0.3 [发行版 10.2 到 11.2]本文档所含信息适用于所有平台 用 ...

  6. UVA 753 A Plug for UNIX (最大流)

    关键在建图,转换器连一条容量无限的边表示可以转化无数次,设备的插头连源点,插座连汇点. dinic手敲已熟练,输出格式又被坑,总结一下,输出空行多case的,一个换行是必要的,最后一个不加空行,有Te ...

  7. vue 不支持 数组Array,只支持get set push,但是正是做tab的时候,用到splice,就都不好用了,最后用v-if,从新渲染 完美解决

    vue 不支持 数组Array,只支持get set push,但是正是做tab的时候,用到splice,就都不好用了,最后用v-if,从新渲染 完美解决

  8. websphere7.0异常:SRVE0255E: 尚未定义要处理 /wcm 的 Web 组/虚拟主机

    websphere7.0错误:SRVE0255E: 尚未定义要处理 /wcm 的 Web 组/虚拟主机. SRVE0255E: 尚未定义要处理 /wcm 的 Web 组/虚拟主机.SRVE0255E: ...

  9. 如何移除 Navicat Premium for Mac 的所有文件

    作者:郭文峰链接:http://www.zhihu.com/question/24210959/answer/34579422来源:知乎著作权归作者所有,转载请联系作者获得授权. 数据库连接信息存放在 ...

  10. 思维 || Make It Equal

    http://codeforces.com/contest/1065/problem/C 题意:给你n个高度分别为a[i]的塔,每次可以横着切一刀,切掉不多于k个塔,问最少切多少刀才能把塔切的都一样高 ...