CSU
来源:互联网 发布:91prom软件下载 编辑:程序博客网 时间:2024/04/30 17:26
Repeated Substrings
CSU - 1632String analysis often arises in applications from biology and chemistry, such as the study of DNA and protein molecules. One interesting problem is to find how many substrings are repeated (at least twice) in a long string. In this problem, you will write a program to find the total number of repeated substrings in a string of at most 100 000 alphabetic characters. Any unique substring that occurs more than once is counted. As an example, if the string is “aabaab”, there are 5 repeated substrings: “a”, “aa”, “aab”, “ab”, “b”. If the string is “aaaaa”, the repeated substrings are “a”, “aa”, “aaa”, “aaaa”. Note that repeated occurrences of a substring may overlap (e.g. “aaaa” in the second case).
The input consists of at most 10 cases. The first line contains a positive integer, specifying the number of
cases to follow. Each of the following line contains a nonempty string of up to 100 000 alphabetic characters.
For each line of input, output one line containing the number of unique substrings that are repeated. You
may assume that the correct answer fits in a signed 32-bit integer.
3aabaabaaaaaAaAaA
545
题意:给出一个字符串,找出出现2次及以上的子串的种数。
后缀数组
跑出sa数组和height数组,然后从i = 2 ~ n遍历,每次 ans += max(0, heigt[i] - lastheight);
这里减掉的是已经计算过的子串种数,可参考比此题稍难的 HDU - 3948 The Number of Palindromes 后缀数组+ST表、distinct substring
#include <iostream>#include <cstdio>#include <string>#include <cstring>using namespace std;typedef long long LL;const int maxn = 1e5 + 8;int sa[maxn], height[maxn];int _rank[maxn], t1[maxn], t2[maxn], c[maxn];string s;inline void get_sa(const int &n, int m){ int i, k, *x = t1, *y = t2, p, j; for(i = 0; i < m; i++) c[i] = 0; for(i = 0; i < n; i++) ++ c[x[i] = s[i]]; for(i = 1; i < m; i++) c[i] += c[i - 1]; for(i = n - 1; i >= 0; i--) sa[-- c[x[i]]] = i; for(k = 1; k <= n; k <<= 1){ p = 0; for(i = n - k; i < n; i++) y[p ++] = i; for(i = 0; i < n; i++) if(sa[i] >= k) y[p ++] = sa[i] - k; for(i = 0; i < m; i++) c[i] = 0; for(i = 0; i < n; i++) ++ c[x[y[i]]]; for(i = 1; i < m; i++) c[i] += c[i - 1]; for(i = n - 1; i >= 0; i--) sa[--c[x[y[i]]]] = y[i]; swap(x, y), p = 1, x[sa[0]] = 0; for(i = 1; i < n; i++) x[sa[i]] = (y[sa[i-1]] == y[sa[i]] && y[sa[i-1]+k] == y[sa[i]+k]) ? p - 1 : p ++; if(p >= n) break; m = p; } k = 0; for(i = 0; i < n; i++) _rank[sa[i]] = i; for(i = 0; i < n; i++){ if(k) --k; if(!_rank[i]) continue; j = sa[_rank[i] - 1]; while(s[i + k] == s[j + k]) k++; height[_rank[i]] = k; }}inline void print(const int &n){ for(int i = 1; i <= n; i++){ //sa and height is 1~n based //cout << i << " : " << _rank[sa[i]] << " " << sa[i] << endl; for(int j = sa[i]; j < n; j++){ //the context is 0~n-1 based cout << s[j]; } cout << endl; } cout << endl;}int main(){ #ifdef LOCAL freopen("10.in", "r", stdin); //freopen("10.out", "w", stdout); #endif // LOCAL ios::sync_with_stdio(false); cin.tie(0); int T, n, sz, i, o, ans; cin >> T; while(T--){ cin >> s; sz = s.size(); n = sz; get_sa(n+1, 256); //print(n); ans = o = 0; for(i = 2; i <= n; i++){ ans += max(0, height[i] - o); o =height[i]; //last line can be changed to "ans += max(0, height[i] - height[i-1]);" } cout << ans << "\n"; } return 0;}
- CSU
- CSU
- CSU
- CSU
- CSU
- CSU
- CSU
- CSU
- CSU
- CSU
- CSU
- CSU
- CSU
- CSU
- CSU
- CSU
- CSU
- CSU
- 数据结构之单链表(c语言版)
- kubernetes 简介:API Server 简介
- 计算机网路基础知识点
- 利用参数宏进行角度与弧度转换
- 高三百日誓师发言稿
- CSU
- 234. Palindrome Linked List
- HDOJ(HDU).1166 敌兵布阵 (ST 单点更新 区间求和)
- JavaScript正则表达式
- I think it
- 正则表达式
- Java面试题-集合
- hdu 1224 Free DIY Tour(dp)
- Ext.form.ComboBox使用方法介绍和级联使用