【SPOJ-DISUBSTR】Distinct Substrings【后缀数组】
来源:互联网 发布:体毛多 知乎 编辑:程序博客网 时间:2024/05/29 02:36
似乎还是例题?
求不同子串的个数。
用总的子串减去重复子串,总的子串个数为n * (n + 1) / 2,重复子串个数就是height的和。
Q:为什么不用std::string了?
A:因为spoj太慢了太慢了太慢了太慢了太慢了
(cnt又开小了,RE一次)
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int maxn = 1005, maxm = 1005, M = 300;int sa[maxn], rank[maxn], height[maxn];int wa[maxn], wb[maxn], wv[maxn], cnt[maxm];void SA(int *r, int n, int m) {int *x = wa, *y = wb;for(int i = 0; i < m; i++) cnt[i] = 0;for(int i = 0; i < n; i++) cnt[x[i] = r[i]]++;for(int i = 1; i < m; i++) cnt[i] += cnt[i - 1];for(int i = n - 1; i >= 0; i--) sa[--cnt[x[i]]] = i;for(int j = 1; j < n; j <<= 1) {int p = 0;for(int i = n - j; i < n; i++) y[p++] = i;for(int i = 0; i < n; i++) if(sa[i] >= j) y[p++] = sa[i] - j;for(int i = 0; i < n; i++) wv[i] = x[y[i]];for(int i = 0; i < m; i++) cnt[i] = 0;for(int i = 0; i < n; i++) cnt[wv[i]]++;for(int i = 1; i < m; i++) cnt[i] += cnt[i - 1];for(int i = n - 1; i >= 0; i--) sa[--cnt[wv[i]]] = y[i];swap(x, y);p = 1; x[sa[0]] = 0;for(int i = 1; i < n; i++)x[sa[i]] = y[sa[i - 1]] == y[sa[i]] && y[sa[i - 1] + j] == y[sa[i] + j] ? p - 1 : p++;if(p >= n) break;m = p;}}void calcHeight(int *r, int n) {int i, j, k;for(i = j = k = 0; i < n; height[rank[i++]] = k)for(k ? k-- : 0, j = sa[rank[i] - 1]; r[i + k] == r[j + k]; k++);}int n, s[maxn];char str[maxn];int main() {int T; scanf("%d", &T);while(T--) {scanf("%s", str); n = strlen(str);for(int i = 0; i < n; i++) s[i] = str[i]; s[n] = 0;SA(s, n + 1, M);for(int i = 0; i <= n; i++) rank[sa[i]] = i;calcHeight(s, n);int ans = n * (n + 1) / 2;for(int i = 1; i <= n; i++) ans -= height[i];printf("%d\n", ans);}return 0;}
0 0
- SPOJ 694 DISUBSTR Distinct Substrings 后缀数组
- [SPOJ DISUBSTR]Distinct Substrings(后缀数组)
- SPOJ DISUBSTR Distinct Substrings (后缀数组)
- 【SPOJ-DISUBSTR】Distinct Substrings【后缀数组】
- 【SPOJ DISUBSTR】Distinct Substrings 后缀数组
- SPOJ DISUBSTR Distinct Substrings(后缀数组)
- SPOJ DISUBSTR Distinct Substrings 后缀数组子串个数
- spoj 694 DISUBSTR - Distinct Substrings (后缀数组)
- SPOJ DISUBSTR Distinct Substrings
- SPOJ DISUBSTR Distinct Substrings
- SPOJ - DISUBSTR Distinct Substrings
- SPOJ DISUBSTR Distinct Substrings
- SPOJ 694 / SPOJ DISUBSTR Distinct Substrings【后缀数组】不相同的子串的个数
- SPOJ694--- DISUBSTR - Distinct Substrings(后缀数组)
- [SPOJ705]DISUBSTR - Distinct Substrings(后缀数组)
- SPOJ - DISUBSTR Distinct Substrings(后缀数组求不相同的子串个数)
- SPOJ DISUBSTR - Distinct Substrings(后缀数组[不相同的子串的个数])
- SPOJ Distinct Substrings 后缀数组
- VMware存储栈那点儿事儿
- Java 方法参数的值传递和引用传递
- Hibernate 与mybatis的区别
- 算法训练 操作格子
- Light OJ 1068 Investigation (数位DP)
- 【SPOJ-DISUBSTR】Distinct Substrings【后缀数组】
- u-boot-2016.01之支持yaffs以及制作补丁
- 程序员修炼之路
- Quartus+Modelsim仿真
- 学习韦东山视频之对u-boot的理解
- 级联菜单(方式一)
- C#中的继承与多态还有接口
- 格式化输入输出易忘点
- Leetcode - 144. Binary Tree Traversal