题意:

对于给出的字符串S, 长度不超过1000,

求其中本质不同的子串的数量, 这些子串满足在字符串S中出现了至少不重合的2次

题解:

将串放入后缀自动机中然后求出每一个节点对应的子串为后缀的子串出现的最早和最晚的位置

然后根据

  1. //return len[last] - len[fail[last]];//多添加一个子串所产生不同子串的个数
  2.  
  3. 然后根据这个改一下
  1. if (R[i] - L[i] > len[fail[i]]) ans += min(len[i], R[i] - L[i]) - len[fail[i]];
  1. #include <set>
  2. #include <map>
  3. #include <stack>
  4. #include <queue>
  5. #include <cmath>
  6. #include <ctime>
  7. #include <cstdio>
  8. #include <string>
  9. #include <vector>
  10. #include <cstring>
  11. #include <iostream>
  12. #include <algorithm>
  13. #include <unordered_map>
  14.  
  15. #define pi acos(-1.0)
  16. #define eps 1e-9
  17. #define fi first
  18. #define se second
  19. #define rtl rt<<1
  20. #define rtr rt<<1|1
  21. #define bug printf("******\n")
  22. #define mem(a, b) memset(a,b,sizeof(a))
  23. #define name2str(x) #x
  24. #define fuck(x) cout<<#x" = "<<x<<endl
  25. #define sfi(a) scanf("%d", &a)
  26. #define sffi(a, b) scanf("%d %d", &a, &b)
  27. #define sfffi(a, b, c) scanf("%d %d %d", &a, &b, &c)
  28. #define sffffi(a, b, c, d) scanf("%d %d %d %d", &a, &b, &c, &d)
  29. #define sfL(a) scanf("%lld", &a)
  30. #define sffL(a, b) scanf("%lld %lld", &a, &b)
  31. #define sfffL(a, b, c) scanf("%lld %lld %lld", &a, &b, &c)
  32. #define sffffL(a, b, c, d) scanf("%lld %lld %lld %lld", &a, &b, &c, &d)
  33. #define sfs(a) scanf("%s", a)
  34. #define sffs(a, b) scanf("%s %s", a, b)
  35. #define sfffs(a, b, c) scanf("%s %s %s", a, b, c)
  36. #define sffffs(a, b, c, d) scanf("%s %s %s %s", a, b,c, d)
  37. #define FIN freopen("../in.txt","r",stdin)
  38. #define gcd(a, b) __gcd(a,b)
  39. #define lowbit(x) x&-x
  40. #define IO iOS::sync_with_stdio(false)
  41.  
  42. using namespace std;
  43. typedef long long LL;
  44. typedef unsigned long long ULL;
  45. const ULL seed = ;
  46. const LL INFLL = 0x3f3f3f3f3f3f3f3fLL;
  47. const int maxn = 1e4 + ;
  48. const int maxm = 8e6 + ;
  49. const int INF = 0x3f3f3f3f;
  50. const int mod = 1e9 + ;
  51.  
  52. struct Suffix_Automaton {
  53. int last, tot, nxt[maxn << ][], fail[maxn << ];//last是未加入此字符前最长的前缀(整个串)所属的节点的编号
  54. int len[maxn << ];// 最长子串的长度 (该节点子串数量 = len[x] - len[fa[x]])
  55. LL num[maxn << ];// 该状态子串的数量
  56. LL maxx[maxn << ];// 长度为x的子串出现次数最多的子串的数目
  57. LL sum[maxn << ];// 该节点后面所形成的自字符串的总数
  58. LL subnum, sublen;// subnum表示不同字符串数目,sublen表示不同字符串总长度
  59. int X[maxn << ], Y[maxn << ]; // Y表示排名为x的节点,X表示该长度前面还有多少个
  60. int L[maxn << ], R[maxn << ];//L表示对应节点代表的数组的最早出现位置,R表示最晚出现位置
  61.  
  62. void init() {
  63. tot = last = ;
  64. fail[] = len[] = ;
  65. for (int i = ; i <= ; i++) nxt[][i] = ;
  66.  
  67. }
  68.  
  69. void extend(int c) {
  70. int u = ++tot, v = last;
  71. for (int i = ; i <= ; i++) nxt[u][i] = ;
  72. fail[u] = ;
  73. L[u] = R[u] = len[u] = len[v] + ;
  74. num[u] = ;
  75. for (; v && !nxt[v][c]; v = fail[v]) nxt[v][c] = u;
  76. if (!v) fail[u] = ;
  77. else if (len[nxt[v][c]] == len[v] + ) fail[u] = nxt[v][c];
  78. else {
  79. int now = ++tot, cur = nxt[v][c];
  80. len[now] = len[v] + ;
  81. memcpy(nxt[now], nxt[cur], sizeof(nxt[cur]));
  82. fail[now] = fail[cur];
  83. fail[cur] = fail[u] = now;
  84. L[now] = L[cur], R[now] = R[cur];
  85. for (; v && nxt[v][c] == cur; v = fail[v]) nxt[v][c] = now;
  86. }
  87. last = u;
  88. //return len[last] - len[fail[last]];
  89. }
  90.  
  91. void get_sa() { // Y表示排名为x的节点,X表示该长度前面还有多少个
  92. for (int i = ; i <= tot; i++) X[i] = ;
  93. for (int i = ; i <= tot; i++) X[len[i]]++;
  94. for (int i = ; i <= tot; i++) X[i] += X[i - ];
  95. for (int i = ; i <= tot; i++) Y[X[len[i]]--] = i;
  96. }
  97.  
  98. int get_L_R() {
  99. int ans = ;
  100. for (int i = tot; i; i--) {
  101. L[fail[Y[i]]] = min(L[fail[Y[i]]], L[Y[i]]);
  102. R[fail[Y[i]]] = max(R[fail[Y[i]]], R[Y[i]]);
  103. }
  104. for (int i = ; i <= tot; i++)//不相交且出现次数至少2的子串个数
  105. if (R[i] - L[i] > len[fail[i]]) ans += min(len[i], R[i] - L[i]) - len[fail[i]];
  106. return ans;
  107. }
  108. } sam;
  109.  
  110. char s[maxn];
  111.  
  112. int main() {
  113. #ifndef ONLINE_JUDGE
  114. FIN;
  115. #endif
  116. while (~sfs(s)) {
  117. if (s[] == '#') break;
  118. sam.init();
  119. int len = strlen(s);
  120. for (int i = ; i < len; ++i) sam.extend((s[i] - 'a'));
  121. sam.get_sa();
  122. printf("%d\n", sam.get_L_R());
  123. }
  124. #ifndef ONLINE_JUDGE
  125. cout << "Totle Time : " << (double) clock() / CLOCKS_PER_SEC << "s" << endl;
  126. #endif
  127. return ;
  128. }

Boring counting HDU - 3518 后缀自动机的更多相关文章

  1. Boring counting HDU - 3518 (后缀自动机)

    Boring counting \[ Time Limit: 1000 ms \quad Memory Limit: 32768 kB \] 题意 给出一个字符串,求出其中出现两次及以上的子串个数,要 ...

  2. Boring counting HDU - 3518 (后缀数组)

    Boring counting \[ Time Limit: 1000 ms \quad Memory Limit: 32768 kB \] 题意 给出一个字符串,求出其中出现两次及以上的子串个数,要 ...

  3. POJ 3518 (后缀自动机)

    POJ 3518 Boring Problem : 给一个串S,询问串S有多个子串出现至少两次且位置不重叠. Solution : 对S串建立后缀自动机,再建立后缀树,dfs一遍统计处每个结点的子树中 ...

  4. HDU 5442 后缀自动机(从环字符串选定一个位置 , 时针或顺时针走一遍,希望得到字典序最大)

    http://acm.hdu.edu.cn/showproblem.php?pid=5442 题目大意: 给定一个字符串,可理解成环,然后选定一位置,逆时针或顺时针走一遍,希望得到字典序最大,如果同样 ...

  5. HDU 4436 (后缀自动机)

    HDU 4436 str2int Problem : 给若干个数字串,询问这些串的所有本质不同的子串转换成数字之后的和. Solution : 首先将所有串丢进一个后缀自动机.由于这道题询问的是不同的 ...

  6. HDU 4622 (后缀自动机)

    HDU 4622 Reincarnation Problem : 给一个串S(n <= 2000), 有Q个询问(q <= 10000),每次询问一个区间内本质不同的串的个数. Solut ...

  7. HDU 4416 (后缀自动机)

    HDU 4416 Good Article Good sentence Problem : 给一个串S,和一些串T,询问S中有多少个子串没有在T中出现. Solution :首先对所有的T串建立后缀自 ...

  8. HDU 5442 后缀自动机+kmp

    题目大意: 给定一个字符串,可理解成环,然后选定一位置,逆时针或顺时针走一遍,希望得到字典序最大,如果同样大,希望找到起始位置最小的,如果还相同,就默认顺时针 比赛一直因为处理最小位置出错,一结束就想 ...

  9. hdu 6208(后缀自动机、或者AC自动机

    题意:给你n个字符串,问你是否存在一个字符串可以从中找到其他n-1个字符串. 思路:其实很简单,找到最长的那个字符串对他进行匹配,看是否能匹配到n-1个字符串. 可以用AC自动机或者后缀自动机做,但是 ...

随机推荐

  1. 数据库(一)—— MySQL介绍

    目录 MySQL介绍 一.MySQL版本 1.mysql主流版本 2.版本选择 二.MySQL连接与实例 1.MySQL的C/S结构 2.MySQL实例 三.mysql三层结构 1.连接层(连接上数据 ...

  2. 推荐几个顶级的IT技术公众号,坐稳了!

    提升自我的路很多,学习是其中最为捷径的一条.丰富的知识提升的不仅仅是你的阅历,更能彰显你的气质,正如古人云:"文质彬彬是君子." 今天为大家整理了10个公众号,分别为多领域,多角度 ...

  3. leetcode.矩阵.240搜索二维矩阵II-Java

    1. 具体题目 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵具有以下特性:每行的元素从左到右升序排列:每列的元素从上到下升序排列. 示例: 现有矩阵 ...

  4. 如何在vue框架中兼容IE

    IE目前已经放弃了自己的独特化,正一步步迎入互联网的主流怀抱.但迫于有用户存在,还是要兼容到IE8,9, 以上. 下面聊一下如何在vue框架中兼容IE 1.首先在index.html <meta ...

  5. vue - blog开发学习3

    1.添加less 和less-loader支持 npm install less less-loader --save-dev 2.新建main.less,将这个样式添加到home.vue中的cont ...

  6. 粗糙的区别prepareStatement:(为Statement的子类)与Statement

    区别: prepareStatement:(为Statement的子类) conn = DBFactory.getInstance().getImpl().getConnection(); //方式一 ...

  7. 【牛客网-剑指offer】跳台阶

    题目: 一只青蛙一次可以跳上1级台阶,也可以跳上2级.求该青蛙跳上一个n级的台阶总共有多少种跳法(先后次序不同算不同的结果). 考点: 递归和循环 思路: 1)利用二叉树,左孩子为跳一级,右孩子为跳两 ...

  8. Vue--入门篇

    一.v-model和单选按钮(radio) <div id="app"> <input name="sex" value="男&qu ...

  9. LeetCode Array Easy 119. Pascal's Triangle II

    Description Given a non-negative index k where k ≤ 33, return the kth index row of the Pascal's tria ...

  10. Shell内置命令expr