CSU 1632 最长公共前缀(出现超过两次的不同子串有多少个)
2016-10-09 18:40
260 查看
1632: Repeated Substrings
Time Limit: 5 Sec Memory Limit:128 MB
Submit: 83 Solved: 33
[Submit][Status][Web
Board]
Description
String 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 writea 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).
Input
The input consists of at most 10 cases. The first line contains a positive integer, specifying the number ofcases to follow. Each of the following line contains a nonempty string of up to 100 000 alphabetic characters.
Output
For each line of input, output one line containing the number of unique substrings that are repeated. Youmay assume that the correct answer fits in a signed 32-bit integer.
Sample Input
3 aabaab aaaaa AaAaA
Sample Output
5 4 5
HINT
height数组为按字典序排列的两两字符串的lcp。
答案为height[i+1]-height[i] (当且仅当(height[i+1]>=height[i]))
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define inf (-((LL)1<<40))
#define lson k<<1, L, (L + R)>>1
#define rson k<<1|1, ((L + R)>>1) + 1, R
#define mem0(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define mem(a, b) memset(a, b, sizeof(a))
#define FIN freopen("in.txt", "r", stdin)
#define FOUT freopen("out.txt", "w", stdout)
#define rep(i, a, b) for(int i = a; i <= b; i ++)
#define dec(i, a, b) for(int i = a; i >= b; i --)
template<class T> T CMP_MIN(T a, T b)
{
return a < b;
}
template<class T> T CMP_MAX(T a, T b)
{
return a > b;
}
template<class T> T MAX(T a, T b)
{
return a > b ? a : b;
}
template<class T> T MIN(T a, T b)
{
return a < b ? a : b;
}
template<class T> T GCD(T a, T b)
{
return b ? GCD(b, a%b) : a;
}
template<class T> T LCM(T a, T b)
{
return a / GCD(a,b) * b;
}
//typedef __int64 LL;
typedef long long LL;
const int MAXN = 110000;
const int MAXM = 110000;
const double eps = 1e-4;
LL MOD = 1000000007;
struct SufArray
{
char s[MAXN];
int sa[MAXN], t[MAXN], t2[MAXN], c[MAXN], n, m;
int rnk[MAXN], height[MAXN];
int mi[MAXN][20], idxK[MAXN];
void init()
{
mem0(s);
mem0(height);
}
void read_str()
{
gets(s);
m = 128;
n = strlen(s);
s[n++] = ' ';
}
void build_sa()
{
int *x = t, *y = t2;
rep (i, 0, m - 1) c[i] = 0;
rep (i, 0, n - 1) c[x[i] = s[i]] ++;
rep (i, 1, m - 1) c[i] += c[i - 1];
dec (i, n - 1, 0) sa[--c[x[i]]] = i;
for(int k = 1; k <= n; k <<= 1)
{
int p = 0;
rep (i, n - k, n - 1) y[p++] = i;
rep (i, 0, n - 1) if(sa[i] >= k) y[p++] = sa[i] - k;
rep (i, 0, m - 1) c[i] = 0;
rep (i, 0, n - 1) c[x[y[i]]] ++;
rep (i, 0, m - 1) c[i] += c[i - 1];
dec (i, n - 1, 0) sa[--c[x[y[i]]]] = y[i];
swap(x, y);
p = 1;
x[sa[0]] = 0;
rep (i, 1, n - 1)
{
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;
}
}
void get_height()
{
int k = 0;
rep (i, 0, n - 1) rnk[sa[i]] = i;
rep (i, 0, n - 1)
{
if(k) k --;
int j = sa[rnk[i] - 1];
while(s[i + k] == s[j + k]) k ++;
height[rnk[i]] = k;
}
}
void rmq_init(int *a, int n)
{
rep (i, 0, n - 1) mi[i][0] = a[i];
for(int j = 1; (1 << j) <= n; j ++)
{
for(int i = 0; i + (1<<j) - 1 < n; i ++)
{
mi[i][j] = min(mi[i][j - 1], mi[i + (1 << (j - 1))][j - 1]);
}
}
rep (len, 1, n)
{
idxK[len] = 0;
while((1 << (idxK[len] + 1)) <= len) idxK[len] ++;
}
}
int rmq_min(int l, int r)
{
int len = r - l + 1, k = idxK[len];
return min(mi[l][k], mi[r - (1 << k) + 1][k]);
}
void lcp_init()
{
get_height();
rmq_init(height, n);
}
int get_lcp(int a, int b)
{
if(a == b) return n - a - 1;
return rmq_min(min(rnk[a], rnk[b]) + 1, max(rnk[a], rnk[b]));
}
void solve()
{
get_height();
LL ans = 0, pre = 0;
rep (i, 1, n - 1)
{
if(height[i] > pre) ans += height[i] - pre;
pre = height[i];
}
cout << ans << endl;
}
};
int T;
SufArray sa;
int main()
{
while(~scanf("%d%*c", &T)) while(T--)
{
sa.init();
sa.read_str();
sa.build_sa();
sa.solve();
}
return 0;
}
/**************************************************************
Problem: 1632
User: skymiange
Language: C++
Result: Accepted
Time:440 ms
Memory:13408 kb
****************************************************************/
相关文章推荐
- UVA 题目1223 - Editor(后缀数组求出现次数超过两次的最长子串的长度)
- spoj220求至少出现两次且不重叠的最长公共子串
- 重复出现超过m次的最长的子串的最大下标 后缀数组或Hash+LCP UVA 12206 - Stammering Aliens
- 动态规划 —— 最长公共子串(输出长度和第一个出现的子串)
- CSU1632Repeated Substrings(后缀数组/最长公共前缀)
- 今天开始学Java 查找两个字符串a,b中的最长公共子串。若有多个,输出在较短串中最先出现的那个。
- SPOJ - PHRASES Relevant Phrases of Annihilation (n个字符串中求至少出现两次且不重叠的最长子串)
- poj 3294 在K个字符串中出现最长公共子串
- 后缀数组 CSU - 1598 最长公共前缀
- 滴血总结(java版):最长公共子序列(子串)、最长公共回文子序列(子串)、最长公共前缀(后缀)
- SPOJ 220 Relevant Phrases of Annihilation(每个字符串至少出现两次且不重叠的最长子串)
- CSU 1598 最长公共前缀 【KMP】
- 后缀数组+最长公共前缀(LCS)+最长重复出现的不重叠字串(POJ 1743)
- URAL1297Palindrome(最长回文子串 、后缀数组最长公共前缀+RMQ)
- spoj 220 Relevant Phrases of Annihilation(每个字符串至少出现两次且不重叠的最长子串)
- CSU 1598 最长公共前缀 KMP连续匹配
- 最长公共前缀子串
- CSU-1598 最长公共前缀
- CSU 1598 最长公共前缀
- POJ 题目3294Life Forms(后缀数组求超过k个的串的最长公共子串)