题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5384

题意:函数f(A, B)定义:A、B为字符串,f(A, B)为A中有多少个不同的B(ex:f("ababa", "aba")==2   f("ababa", "bab")==1),现在给你一组A串,一组B串,问你对每个A串的是多少

解:ac自动机模板题,可以把A串用'z'+1,连成一个串处理起来比较方便

  1. /*
  2. * Problem: hdu5384 Danganronpa
  3. * Author: SHJWUDP
  4. * Created Time: 2015/8/13 星期四 14:38:23
  5. * File Name: 1006.cpp
  6. * State: Accepted
  7. * Memo: ac自动机
  8. */
  9. #include <iostream>
  10. #include <cstdio>
  11. #include <vector>
  12. #include <cstring>
  13. #include <algorithm>
  14. #include <queue>
  15.  
  16. using namespace std;
  17.  
  18. const int MaxA=1e5+;
  19. const int MaxB=7e5+;
  20.  
  21. const int SIGMA_SIZE=;
  22.  
  23. struct AhoCorasickAutomata {
  24. int ch[MaxB][SIGMA_SIZE];
  25. int val[MaxB];
  26. int sz;
  27. int f[MaxB], last[MaxB];
  28.  
  29. int newNode() {
  30. memset(ch[sz], , sizeof(ch[sz]));
  31. val[sz]=;
  32. return sz++;
  33. }
  34.  
  35. void init() {
  36. sz=;
  37. newNode();
  38. }
  39.  
  40. int id(char c) {
  41. return c-'a';
  42. }
  43.  
  44. void insert(char* p) {
  45. int u=;
  46. while(*p) {
  47. int c=id(*p++);
  48. if(!ch[u][c]) ch[u][c]=newNode();
  49. u=ch[u][c];
  50. }
  51. val[u]++;
  52. }
  53.  
  54. void getFail() {
  55. queue<int> Q;
  56. f[]=;
  57. for(int c=; c<SIGMA_SIZE; c++) {
  58. int u=ch[][c];
  59. if(u) { f[u]=; Q.push(u); last[u]=; }
  60. }
  61.  
  62. while(!Q.empty()) {
  63. int r=Q.front(); Q.pop();
  64. for(int c=; c<SIGMA_SIZE; c++) {
  65. int u=ch[r][c];
  66. if(!u) { ch[r][c]=ch[f[r]][c]; continue; }
  67. Q.push(u);
  68. int v=f[r];
  69. while(v && !ch[v][c]) v=f[v];
  70. f[u]=ch[v][c];
  71. last[u]=val[f[u]]?f[u]:last[f[u]];
  72. }
  73. }
  74. }
  75.  
  76. long long dealAns(int u) {
  77. long long res=;
  78. while(u) {
  79. res+=val[u];
  80. // val[u]=0;
  81. u=last[u];
  82. }
  83. return res;
  84. }
  85.  
  86. void find(char * T, long long * ans) {
  87. int u=;
  88. int pos=;
  89. long long sum=, ltsum=;
  90. for(int i=; T[i]; i++) {
  91. int c=id(T[i]);
  92. u=ch[u][c];
  93. if(val[u]) sum+=dealAns(u);
  94. else if(last[u]) sum+=dealAns(last[u]);
  95. if(T[i]=='z'+) {
  96. ans[pos++]+=sum-ltsum;
  97. ltsum=sum;
  98. }
  99. }
  100. }
  101. } ac;
  102.  
  103. int n, m;
  104. char str[MaxB];
  105. long long ans[MaxA];
  106. char gogogo[MaxA];
  107. int main() {
  108. #ifndef ONLINE_JUDGE
  109. freopen("in", "r", stdin);
  110. //freopen("out", "w", stdout);
  111. #endif
  112. int T;
  113. scanf("%d", &T);
  114. while(T--) {
  115. scanf("%d%d", &n, &m);
  116. int len=;
  117. for(int i=; i<n; i++) {
  118. scanf("%s", str+len);
  119. while(str[len]) len++;
  120. str[len++]='z'+;
  121. }
  122. str[len]='\0';
  123. ac.init();
  124. for(int i=; i<m; i++) {
  125. scanf("%s", gogogo);
  126. ac.insert(gogogo);
  127. }
  128. ac.getFail();
  129. memset(ans, , sizeof(ans));
  130. ac.find(str, ans);
  131. for(int i=; i<=n; i++) {
  132. printf("%I64d\n", ans[i]);
  133. }
  134. }
  135. return ;
  136. }

[2015hdu多校联赛补题]hdu5384 Danganronpa的更多相关文章

  1. [2015hdu多校联赛补题]hdu5302 Connect the Graph

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5302 题意:给你一个无向图,它的边要么是黑色要么是白色,且图上的每个点最多与两个黑边两个白边相连.现在 ...

  2. [2015hdu多校联赛补题]hdu5301 Buildings

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5301 题目大意:给你一块由1x1方格组成的矩形区域,其中有且仅有一个坏块,现在你要在上面建矩形的房子, ...

  3. [2015hdu多校联赛补题]hdu5378 Leader in Tree Land

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5378 题意:给你一棵n个结点的有根树.因为是有根树,那么每个结点可以指定以它为根的子树(后面讨论的子树 ...

  4. [2015hdu多校联赛补题]hdu5372 Segment Game

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5372 题意:进行n次操作,操作分两种,0和1,每一个0操作按出现顺序有一个编号(从1开始 0操作 0 ...

  5. [2015hdu多校联赛补题]hdu5371 Hotaru's problem

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5371 题意:把一个数字串A翻过来(abc翻过来为cba)的操作为-A,我们称A-AA这样的串为N-se ...

  6. [2015hdu多校联赛补题]hdu5303 Delicious Apples

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5303 题意:在一个长为L的环形路径上种着一些苹果树,告诉你苹果树的位置(题目中以0~L指示坐标)及苹果 ...

  7. [2015hdu多校联赛补题]hdu5299 Circles Game

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5299 题意: 在欧几里得平面上有n个圆,圆之间不会相交也不会相切,现在Alice和Bob玩游戏,两人轮 ...

  8. [2015hdu多校联赛补题]hdu5348 MZL's endless loop

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5348 题意:给你一个无向图,要你将无向图的边变成有向边,使得得到的图,出度和入度差的绝对值小于等于1, ...

  9. [2015hdu多校联赛补题]hdu5324 Boring Class

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5324 题意:给你一个二维的序列,让你找出最长的第一维升第二维降的子序列(如果多个答案,输出字典序最小) ...

随机推荐

  1. cocos2dx day 3 - Chapter5 Scene

    写在前面 越来越懒了,才3天,主要是cocos2dx官网的文章写的还是不是太完美,发现一段代码有个笔误,还有好几处写得不是很清楚的,所以有点泄气,不想继续读下去,不过为了我的第一款手游,一切困难都要先 ...

  2. sqlce中不支持sp_rename修改表名

    The sp_rename procedure is not avialable in SQL CE! In Sql Server 2005 Management Studio you have to ...

  3. android studio 生成aar包并在其他工程引用 (导入)aar包

    1.aar包是Android studio下打包android工程中src.res.lib后生成的aar文件,aar包导入其他android studio 工程后,其他工程可以方便引用源码和资源文件 ...

  4. 如何安装NodeJS到阿里云Centos (64位版本V5-7)

    如何安装NodeJS到阿里云Centos (64位版本V5-7) (Centos与Red Hat® Enterprise Linux® / RHEL, Fedora属于一类) 1) 安装v0.10版 ...

  5. 自动化测试 using System.Windows.Automation;

    frameworke3.0 及以上 using System.Windows.Automation; UIAutomationClient.dll UIAutomationClientsideProv ...

  6. 诺基亚N900使用技巧

    一直都对 Linux 的掌上终端挺感冒的,最近从闲鱼上入后一台欧版的,开机进行评测和使用.以下经验仅供新手参考. 选择 N900 是有原因的,首先 N900 相对来说比较小巧(作为 MID,对比起手机 ...

  7. Javascript BOM对象

    BOM是browser object model的缩写,简称浏览器对象模型. window对象(BOM的核心对象) 表示浏览器的一个实例,在浏览器中,window对象有着双重角色,它既是通过Javas ...

  8. javascript 字符串数组链接

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  9. .NET (二)委托第二讲:内置委托Func

    在上一章节中,我们自己声明了一个委托: public delegate bool Cal(int num); 接受int参数,返回bool类型,目的是过滤集合中的 奇数 或者 偶数. .NET 为我们 ...

  10. 在js中怎么样选择互斥的相邻元素

    在使用jquery中,我们通常会选择siblings()去选择相邻元素,使用eq()方法去匹配元素,使用index()获取对应元素的索引值,具体jquery代码如下: <style> *{ ...