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

来源:互联网 发布:数据库搭建流程 编辑:程序博客网 时间:2024/05/04 18:57

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


1 0
原创粉丝点击