题目

  1. Oimaster and sevenk love each other.
  2. 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

题解

广义后缀自动机裸题

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

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

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cmath>
  4. #include<string>
  5. #include<cstring>
  6. #include<algorithm>
  7. #define LL long long int
  8. #define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
  9. #define REP(i,n) for (int i = 1; i <= (n); i++)
  10. #define BUG(s,n) for (int i = 1; i <= (n); i++) cout<<s[i]<<' '; puts("");
  11. using namespace std;
  12. const int maxn = 200005,maxm = 400005,INF = 1000000000;
  13. inline int read(){
  14. int out = 0,flag = 1; char c = getchar();
  15. while (c < 48 || c > 57){if (c == '-') flag = -1; c = getchar();}
  16. while (c >= 48 && c <= 57){out = (out << 3) + (out << 1) + c - 48; c = getchar();}
  17. return out * flag;
  18. }
  19. int n;
  20. int ch[maxn][26],pre[maxn],step[maxn],cnt,last;
  21. int sz[maxn],vis[maxn];
  22. char ss[maxn >> 1];
  23. string s[10005];
  24. void ins(int x){
  25. int p = last,np = ++cnt; step[np] = step[p] + 1; last = np;
  26. while (p && !ch[p][x]) ch[p][x] = np,p = pre[p];
  27. if (!p) pre[np] = 1;
  28. else {
  29. int q = ch[p][x];
  30. if (step[q] == step[p] + 1) pre[np] = q;
  31. else {
  32. int nq = ++cnt; step[nq] = step[p] + 1;
  33. for (int i = 0; i < 26; i++) ch[nq][i] = ch[q][i];
  34. pre[nq] = pre[q]; pre[np] = pre[q] = nq;
  35. while (ch[p][x] == q) ch[p][x] = nq,p = pre[p];
  36. }
  37. }
  38. }
  39. int main(){
  40. n = read();
  41. int q = read();
  42. cnt = last = 1;
  43. for (int i = 1; i <= n; i++){
  44. last = 1;
  45. scanf("%s",ss); int len = strlen(ss);
  46. s[i] = (string)(ss);
  47. for (int i = 0; i < len; i++) ins(ss[i] - 'a');
  48. }
  49. int u;
  50. for (int i = 1; i <= n; i++){
  51. u = 1;
  52. for (unsigned int j = 0; j < s[i].length(); j++){
  53. u = ch[u][s[i][j] - 'a'];
  54. for (int p = u; p && vis[p] != i; p = pre[p])
  55. sz[p]++,vis[p] = i;
  56. }
  57. }
  58. while (q--){
  59. scanf("%s",ss);
  60. int len = strlen(ss),ans = 0;
  61. u = 1;
  62. for (int i = 0; i < len; i++){
  63. if (!(u = ch[u][ss[i] - 'a'])) break;
  64. if (i == len - 1) ans = sz[u];
  65. }
  66. printf("%d\n",ans);
  67. }
  68. return 0;
  69. }

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. AS学习系列[1]——初识Android Studio

    写在前面的话:由于于方老师的高墙所限,网络成了学习Android第一道“拦路虎”.所以,个人以为,在学习Android之前需要了解下FQ技术(仅仅是为了技术学习). 1.AS AS(Android s ...

  2. SQL增删查改语句

    一.增:有4种方法 1.使用insert插入单行数据: 语法:insert [into] <表名> [列名] values <列值> insert into sheet1 va ...

  3. Mybatis-Generator逆向生成Po,Mapper,XMLMAPPER(idea)

    前文有一篇手工生成的说明,地址: http://www.cnblogs.com/xiaolive/p/4874605.html, 现在这个补充一下在idea里面的自动版本的数据库逆向生成工具: 一.g ...

  4. make 与makefile(会不会写 makefile,从一个侧面说明了一个人是否具备完成大型工程的能力。)

    跟我一起写 Makefile /**/ 陈皓 (CSDN) 概述 —— 什么是makefile?或许很多Winodws的程序员都不知道这个东西,因为那些Windows的IDE都为你做了这个工作,但我觉 ...

  5. Codeforces Round #317 div2 E div1 C CNF 2 (图论,匹配)

      CNF 2 'In Boolean logic, a formula is in conjunctive normal form (CNF) or clausal normal form if i ...

  6. 签名ipa,让其它手机也安装

    开发的时候,需要将app让其它人装上测试,虽然通过xcode可以使用编译进去,但是仍显不方便. 网上有个工具, http://code.google.com/p/iresign/ 通过这个工具,使用自 ...

  7. OpenCascade:屏闪问题。

    1.在OnDraw中同时调用用V3d_View::Redaw()和 V3d_View::FitAll();可暂时解决. 2.在OnDraw中同时调用用V3d_View::Update();

  8. 8 Java 归并排序(MergerSort)

    图片素材与文字描述来自:尚硅谷-韩顺平数据结构与算法. 1.基本思想 归并排序是利用归并的思想实现的排序方法,该算法采用经典的分治(divide-and-conquer)策略(分治法将问题分(divi ...

  9. Ueditor1.4.3上传视频IE下无法播放的问题

    一:百度编辑器插入视频后,自动生成一段代码: <video class="edui-upload-video vjs-default-skin video-js" contr ...

  10. Python IDE推荐

    八个最佳Python IDE 作者:chszs,转载需注明.博客主页:http://blog.csdn.net/chszs Python是一种功能强大.语言简洁的编程语言.本文向大家推荐8个适合pyt ...