SPOJ 694. Distinct Substrings,705. New Distinct Substrings(字尾陣列)

畫船聽雨發表於2015-01-22

題目大意:給定長度為N的字串,求出其中不相同子串的個數。

解題思路:每一個字串一定是某個字尾的字首,那麼原問題就可以等價於求所有字尾之間的不相同的字首的個數。如果所有的字尾按照suffix(sa[1]),suffix(sa[2])……suffix(sa[n])的順序計算,我們會發現對於每個新加進來的字尾suffix(sa[k]),它將產生n-sa[k]+1個新的字首。但是其中有leight[k]個是和前面的字串的字首是相同的。所以suffix(sa[k])加進來增加的不同的子串的個數為n-sa[k]+1-height[k]。累加起來就是所有字串的數目了。


705. New Distinct Substrings

Problem code: SUBST1

Given a string, we need to find the total number of its distinct substrings.

Input

T- number of test cases. T<=20;Each test case consists of one string, whose length is <= 50000

Output

For each test case output one number saying the number of distinct substrings.

Example

Input:
2
CCCCC
ABABA

Output:
5
9
#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <iomanip>
#include <stdio.h>
#include <string>
#include <queue>
#include <cmath>
#include <stack>
#include <ctime>
#include <map>
#include <set>
#define eps 1e-9
///#define M 1000100
///#define LL __int64
#define LL long long
///#define INF 0x7ffffff
#define INF 0x3f3f3f3f
#define PI 3.1415926535898
#define zero(x) ((fabs(x)<eps)?0:x)
#define mod 1000000007
#define Read() freopen("autocomplete.in","r",stdin)
#define Write() freopen("autocomplete.out","w",stdout)
#define Cin() ios::sync_with_stdio(false)

using namespace std;



inline int read()
{
    char ch;
    bool flag = false;
    int a = 0;
    while(!((((ch = getchar()) >= '0') && (ch <= '9')) || (ch == '-')));
    if(ch != '-')
    {
        a *= 10;
        a += ch - '0';
    }
    else
    {
        flag = true;
    }
    while(((ch = getchar()) >= '0') && (ch <= '9'))
    {
        a *= 10;
        a += ch - '0';
    }
    if(flag)
    {
        a = -a;
    }
    return a;
}
void write(int a)
{
    if(a < 0)
    {
        putchar('-');
        a = -a;
    }
    if(a >= 10)
    {
        write(a / 10);
    }
    putchar(a % 10 + '0');
}

const int maxn = 50010;

int wa[maxn], wb[maxn], wv[maxn], ws1[maxn];
int sa[maxn];
int cmp(int *r, int a, int b, int l)
{
    return r[a]==r[b]&&r[a+l]==r[b+l];
}
void da(int *r,int *sa,int n,int m)
{
    int i, j, p, *x = wa,*y = wb;

    for(i = 0; i<m; i++) ws1[i]=0;
    for(i = 0; i<n; i++) ws1[x[i]=r[i]]++;
    for(i = 1; i<m; i++) ws1[i]+=ws1[i-1];
    for(i = n-1; i>=0; i--) sa[--ws1[x[i]]]=i;

    for(j = 1, p = 1; p<n; j*=2, m=p)
    {
        for(p = 0, i = n-j; i<n; i++) y[p++]=i;

        for(i = 0; i<n; i++)
            if(sa[i]>=j) y[p++]=sa[i]-j;
        for(i = 0; i<n; i++) wv[i]=x[y[i]];
        for(i = 0; i<m; i++) ws1[i]=0;
        for(i = 0; i<n; i++) ws1[wv[i]]++;
        for(i = 1; i<m; i++) ws1[i]+=ws1[i-1];
        for(i = n-1; i>=0; i--) sa[--ws1[wv[i]]]=y[i];
        for(swap(x,y),p=1,x[sa[0]]=0,i=1; i<n; i++)
            x[sa[i]] = cmp(y,sa[i-1],sa[i],j)?p-1:p++;
    }
    return;
}

int rank[maxn],height[maxn];
void calheight(int *r,int *sa,int n)
{
    int i,j,k=0;
    for(i = 1; i<=n; i++) rank[sa[i]]=i;
    for(i = 0; i<n; height[rank[i++]]=k)
        for(k?k--:0, j=sa[rank[i]-1]; r[i+k] == r[j+k]; k++);
    return;
}

int sqe[maxn];   


bool judge(int mid, int n)
{
    int Max, Min;
    for(int i = 2; i <= n; i++)
    {
        Max = sa[i-1];
        Min = sa[i-1];
        while(height[i] >= mid)
        {
            Max = max(sa[i], Max);
            Min = min(sa[i], Min);
            i++;
        }
        if(Max - Min > mid) return true;
    }
    return false;
}

char str[maxn];

int main()
{
    ///Cin();
    int T;
    cin >>T;
    while(T--)
    {
        cin >>str;
        int len = strlen(str);
        for(int i = 0; i < len; i++)
        sqe[i] = str[i];
        sqe[len] = 0;

        da(sqe, sa, len+1, 200);
        calheight(sqe, sa, len);
        int ans = 0;
        for(int i = 0; i < len; i++)
        ans += len-i-height[rank[i]];
        cout<<ans<<endl;
    }
    return 0;
}


相關文章