码迷,mamicode.com
首页 > 其他好文 > 详细

spoj694 DISUBSTR - Distinct Substrings

时间:2018-01-11 11:32:13      阅读:143      评论:0      收藏:0      [点我收藏+]

标签:names   string   pre   ios   ace   div   tin   char   print   

不相同子串个数。
参考那篇论文。

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
int n, m, p, x[1005], y[1005], c[1005], sa[1005], rnk[1005], hei[1005];
int ans, T;
char s[1005];
void getSa(){
    s[n++] = 0;
    for(int i=0; i<m; i++)  c[i] = 0;
    for(int i=0; i<n; i++)  c[x[i]=s[i]]++;
    for(int i=1; i<m; i++)  c[i] += c[i-1];
    for(int i=n-1; i>=0; i--)   sa[--c[x[i]]] = i;
    for(int j=1; p<n; j*=2, m=p){
        p = 0;
        for(int i=n-j; i<n; i++)    y[p++] = i;
        for(int i=0; i<n; i++)  if(sa[i]>=j)    y[p++] = sa[i] - j;
        for(int i=0; i<m; i++)  c[i] = 0;
        for(int i=0; i<n; i++)  c[x[y[i]]]++;
        for(int i=1; i<m; i++)  c[i] += c[i-1];
        for(int i=n-1; i>=0; i--)   sa[--c[x[y[i]]]] = y[i];
        swap(x, y);
        x[sa[0]] = 0;
        p = 1;
        for(int i=1; i<n; i++)
            if(y[sa[i-1]]==y[sa[i]] && y[sa[i-1]+j]==y[sa[i]+j])
                x[sa[i]] = p - 1;
            else
                x[sa[i]] = p++;
    }
    n--;
}
void getHei(){
    int h=0;
    for(int i=1; i<=n; i++) rnk[sa[i]] = i;
    for(int i=0; i<n; i++){
        if(h)   h--;
        int j=sa[rnk[i]-1];
        while(s[i+h]==s[j+h])   h++;
        hei[rnk[i]] = h;
    }
}
int main(){
    cin>>T;
    while(T--){
        scanf("%s", s);
        n = strlen(s);
        m = 128;
        p = ans = 0;
        getSa();
        getHei();
        for(int i=1; i<=n; i++)
            ans += n - sa[i] - hei[i];//想一想,为什么
        printf("%d\n", ans);
    }
    return 0;
}

spoj694 DISUBSTR - Distinct Substrings

标签:names   string   pre   ios   ace   div   tin   char   print   

原文地址:https://www.cnblogs.com/poorpool/p/8266767.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!