CSU 1632 最长公共前缀(出现超过两次的不同子串有多少个)

来源:互联网 发布:linux 数据库 编辑:程序博客网 时间:2024/05/16 12:00

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 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).

Input

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.

Output

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.

Sample Input

3aabaabaaaaaAaAaA

Sample Output

545

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****************************************************************/



0 0