SPOJ 694. Distinct Substrings,705. New Distinct Substrings(后缀数组)

题目大意:给定长度为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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值