• SPOJ SUBST1 后缀数组


    题目链接:http://www.spoj.com/problems/SUBST1/en/

    题意:给定一个字符串,求不相同的子串个数。

    思路:直接根据09年oi论文<<后缀数组——出来字符串的有力工具>>的解法。

    此题和SPOJ DISUBSTR一样,至少数据范围变大了。

    #define _CRT_SECURE_NO_DEPRECATE
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<string>
    #include<queue>
    #include<vector>
    #include<time.h>
    #include<cmath>
    using namespace std;
    typedef long long int LL;
    const int MAXN = 50000 + 5;
    int cmp(int *r, int a, int b, int l){
        return r[a] == r[b] && r[a + l] == r[b + l];
    }
    int wa[MAXN], wb[MAXN], wv[MAXN], WS[MAXN];
    void da(int *r, int *sa, int n, int m){
        int i, j, p, *x = wa, *y = wb, *t;
        for (i = 0; i<m; i++) WS[i] = 0;
        for (i = 0; i<n; i++) WS[x[i] = r[i]]++;
        for (i = 1; i<m; i++) WS[i] += WS[i - 1];
        for (i = n - 1; i >= 0; i--) sa[--WS[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++) WS[i] = 0;
            for (i = 0; i<n; i++) WS[wv[i]]++;
            for (i = 1; i<m; i++) WS[i] += WS[i - 1];
            for (i = n - 1; i >= 0; i--) sa[--WS[wv[i]]] = y[i];
            for (t = x, x = y, y = t, 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], sa[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;
    }
    void solve(int n){
        int ans = 0;
        for (int i = 1; i <= n; i++){
            ans += ((n - 1) - sa[i] + 1 - height[i]);
        }
        printf("%d
    ", ans);
    }
    int t, len, r[MAXN];
    char str[MAXN];
    int main(){
        //#ifdef kirito
        //    freopen("in.txt","r",stdin);
        //    freopen("out.txt","w",stdout);
        //#endif
        //    int start = clock();
        scanf("%d", &t);
        while (t--){
            scanf("%s", str); len = strlen(str);
            for (int i = 0; i <= len; i++){
                if (i == len){ r[i] = 0; continue; }
                r[i] = (int)str[i];
            }
            da(r, sa, len + 1, 256);
            calheight(r, sa, len);
            solve(len);
        }
        //#ifdef LOCAL_TIME
        //    cout << "[Finished in " << clock() - start << " ms]" << endl;
        //#endif
        return 0;
    }
  • 相关阅读:
    C# 获取 PC 序列号
    C# 获取进程退出代码
    PHP mail() 函数
    PHP ezmlm_hash() 函数
    PHP Mail 函数
    PHP libxml_clear_errors() 函数
    PHP setrawcookie() 函数
    创建用户
    [FJOI2018]领导集团问题
    C# 获取进程退出代码
  • 原文地址:https://www.cnblogs.com/kirito520/p/5756407.html
Copyright © 2020-2023  润新知