题意:略

在ac自动机上,一个节点出现的次数等于能通过fail到它的节点的次数之和。而叶节点就等于它被爬过的次数。

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cmath>
  4. #include <algorithm>
  5. #include <vector>
  6. #include <iomanip>
  7. #include <cstring>
  8. #include <map>
  9. #include <queue>
  10. #include <set>
  11. #include <cassert>
  12. #include <stack>
  13. #include <bitset>
  14. #define mkp make_pair
  15. using namespace std;
  16. const double EPS=1e-;
  17. typedef long long lon;
  18. const lon SZ=,SSZ=,APB=,one=,INF=0x7FFFFFFF,mod=;
  19. int n,nex[SZ][APB],cnt,num[SZ];
  20. int fail[SZ],match[SZ],in[SZ],ans[SZ];
  21. char ch[SZ];
  22.  
  23. void build(int x)
  24. {
  25. int cur=;
  26. for(int i=;ch[i];++i)
  27. {
  28. int c=ch[i]-'a';
  29. if(!nex[cur][c])nex[cur][c]=++cnt;
  30. cur=nex[cur][c];
  31. ++num[cur];
  32. //cout<<num[cur]<<endl;
  33. }
  34. match[x]=cur;
  35. }
  36.  
  37. void build_fail()
  38. {
  39. queue<int> q;
  40. q.push();
  41. for(;q.size();)
  42. {
  43. int fr=q.front();
  44. q.pop();
  45. for(int i=;i<APB;++i)
  46. {
  47. if(nex[fr][i])
  48. {
  49. int u=nex[fr][i];
  50. if(fr==)
  51. {
  52. fail[u]=;
  53. }
  54. else
  55. {
  56. int v=fail[fr];
  57. for(;!nex[v][i]&&v;v=fail[v]);
  58. if(nex[v][i])fail[u]=nex[v][i];
  59. else fail[u]=;
  60. }
  61. q.push(u);
  62. }
  63. }
  64. }
  65. }
  66.  
  67. void topo()
  68. {
  69. for(int i=;i<=cnt;++i)
  70. {
  71. ++in[fail[i]];
  72. }
  73. stack<int> stk;
  74. for(int i=;i<=cnt;++i)
  75. {
  76. if(!in[i])
  77. {
  78. stk.push(i);
  79. //cout<<"i: "<<i<<endl;
  80. }
  81. ans[i]=num[i];
  82. }
  83. for(;stk.size();)
  84. {
  85. int top=stk.top();
  86. stk.pop();
  87. --in[fail[top]],ans[fail[top]]+=ans[top];
  88. if(!in[fail[top]])
  89. {
  90. stk.push(fail[top]);
  91. }
  92. }
  93. }
  94.  
  95. void init()
  96. {
  97. cin>>n;
  98. for(int i=;i<=n;++i)
  99. {
  100. cin>>ch+;
  101. build(i);
  102. }
  103. build_fail();
  104. topo();
  105. for(int i=;i<=n;++i)
  106. {
  107. cout<<ans[match[i]]<<endl;
  108. }
  109. }
  110.  
  111. void work()
  112. {
  113.  
  114. }
  115.  
  116. int main()
  117. {
  118. std::ios::sync_with_stdio();
  119. //freopen("d:\\1.txt","r",stdin);
  120. int casenum;
  121. //cin>>casenum;
  122. //cout<<casenum<<endl;
  123. //for(int time=1;time<=casenum;++time)
  124. //for(int time=1;cin>>n>>qnum,n;++time)
  125. {
  126. //cout<<"Case "<<time<<": ";
  127. init();
  128. work();
  129. }
  130. return ;
  131. }

hsy单词的更多相关文章

  1. java统计字符串单词的个数

    在一些项目中可能需要对一段字符串中的单词进行统计,我在这里写了一个简单的demo,有需要的同学可以拿去看一下. 本人没怎么写个播客,如果有啥说的不对的地方,你来打我啊 不说废话了直接贴代码: 实现代码 ...

  2. JavaScript将字符串中的每一个单词的第一个字母变为大写其余均为小写

    要求: 确保字符串的每个单词首字母都大写,其余部分小写. 这里我自己写了两种方法,或者说是一种方法,另一个是该方法的变种. 第一种: function titleCase(str) { var new ...

  3. BZOJ 3172: [Tjoi2013]单词 [AC自动机 Fail树]

    3172: [Tjoi2013]单词 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 3198  Solved: 1532[Submit][Status ...

  4. [LeetCode] Concatenated Words 连接的单词

    Given a list of words (without duplicates), please write a program that returns all concatenated wor ...

  5. [LeetCode] Word Squares 单词平方

    Given a set of words (without duplicates), find all word squares you can build from them. A sequence ...

  6. [LeetCode] Valid Word Square 验证单词平方

    Given a sequence of words, check whether it forms a valid word square. A sequence of words forms a v ...

  7. [LeetCode] Minimum Unique Word Abbreviation 最短的独一无二的单词缩写

    A string such as "word" contains the following abbreviations: ["word", "1or ...

  8. [LeetCode] Valid Word Abbreviation 验证单词缩写

    Given a non-empty string s and an abbreviation abbr, return whether the string matches with the give ...

  9. [LeetCode] Maximum Product of Word Lengths 单词长度的最大积

    Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the tw ...

随机推荐

  1. FB面经 Prepare: Count Unique Island

    数unique island, 比如 110000 110001 001101 101100 100000 总共两个unique岛,不是四个 方法可以是记录每次新的岛屿搜索的路径,left,right ...

  2. #WEB安全基础 : HTTP协议 | 0x15 HTTPS:给你点颜色看看

    "你好,我是HTTPS,我只是披了个外壳,我还是原来的HTTP." 这是HTTPS叫我转达给你的话. HTTPS本质上就是HTTP,只不过加了点调料.它比HTTP更安全,使用了加密 ...

  3. 水管工游戏——dfs

    问题描述: 水管工游戏是指如下图中的矩阵中,一共有两种管道,一个是直的,一个是弯的,所有管道都可以自由旋转,最终就是要连通入水口可出水口.其中的树为障碍物. 方案: 输入格式:输入的第一行为两个整数N ...

  4. Let'sEncrypt 免费通配符/泛域名SSL证书添加使用教程

    Let'sEncrypt 免费通配符/泛域名SSL证书添加使用教程 通配符证书一般还是比较贵的一般最便宜的通配符证书5.60美元一年,只不过Let'sEncrypt的有效期是3个月,对于一般用户来说基 ...

  5. CentOS 7Google浏览器

    CentOS 7安装并启动Google浏览器(★firecat亲测有效★) 2018年09月23日 12:42:47 libaineu2004 阅读数:3088    版权声明:本文为博主原创文章,未 ...

  6. cent OS 7查询IP

    环境: win7旗舰版 VMware Workstation Pro (虚拟机软件) CentOS-7-x86_64-DVD-1804.iso 安装时选择了默认配置,最小系统安装. 安装好后用  if ...

  7. cxgrid 非编辑状态下复制当前列的值 真折腾人

    1.自带的CTRL +C 只能复制整行,不知是不是版本问题. 2.有分组这个代码就不行了 s:= G1DBView.DataController.Values[G1DBView.Controller. ...

  8. ltp-ddt nor qspi spi调试中需要修改的地方

    1 blk_device_dd_readwrite_test.sh before SRC_FILE="/home/root/srctest_file_${DEVICE_TYPE}_$$&qu ...

  9. AutoCAD LT 2019 安装教程

    autocad lt 2019是mac平台上一款全球领先的更快.更精确地创建二维图形设计平台,用于二维草图.图形和文档编制,广泛应用于机械设计.工业制图.工程制图.土木建筑.装饰装潢.服装加工等多个行 ...

  10. github收藏夹

    技术面试必备基础知识 https://github.com/CyC2018/CS-Notes 程序员简历模板列表 https://github.com/geekcompany/ResumeSample ...