码迷,mamicode.com
首页 > 编程语言 > 详细

后缀数组(板子)

时间:2019-02-11 17:19:31      阅读:226      评论:0      收藏:0      [点我收藏+]

标签:name   后缀   scanf   turn   过程   临时   scan   eof   space   

贴一下防止忘掉。

#include <bits/stdc++.h>
#define N 1000010
using namespace std;

char s[N];
int n, m, sa[N], tp[N], rk[N], _rk[N], bin[N];

void base_sort () {
    for (int i = 1; i <= m; ++i) bin[i] = 0;
    for (int i = 1; i <= n; ++i) bin[rk[tp[i]]]++; //每个出现的第一关键字++
    for (int i = 1; i <= m; ++i) bin[i] += bin[i - 1]; //前缀和 -> rk为i的后缀排名是多少
    for (int i = n; i >= 1; --i) sa[bin[rk[tp[i]]]--] = tp[i]; // 先记大的,然后名次顺位递减
}

void suffix_sort () {
    m = 255;
    for (int i = 1; i <= n; ++i) {
        rk[i] = s[i - 1], tp[i] = i;
    }
    base_sort ();
    for (int w = 1; w <= n; w <<= 1) {
        int cnt = 0;
        for (int i = n - w + 1; i <= n; ++i) {
            tp[++cnt] = i; //莫得第二关键字
        }
        for (int i = 1; i <= n; ++i) {
            if (sa[i] > w) { //如果有第二关键字
                tp[++cnt] = sa[i] - w; //把i当做第二关键字の后缀
            }
        }
        //printf ("sa : "); for (int i = 1; i <= n; ++i) printf ("%d ", sa[i]); printf ("\n");
        //printf ("rk : "); for (int i = 1; i <= n; ++i) printf ("%d ", rk[i]); printf ("\n");
        base_sort (); cnt = 1;
        //printf ("sa : "); for (int i = 1; i <= n; ++i) printf ("%d ", sa[i]); printf ("\n");
        //printf ("rk : "); for (int i = 1; i <= n; ++i) printf ("%d ", rk[i]); printf ("\n");
        memcpy (_rk, rk, sizeof (rk)); //把rk临时放一边
        rk [sa[1]] = 1;
        for (int i = 2; i <= n; ++i) {
            rk[sa[i]] = _rk[sa[i]] == _rk[sa[i - 1]] && _rk[sa[i] + w] == _rk[sa[i - 1] + w] ? cnt : ++cnt;
            //验证后缀sa[i]是不是和sa[i - 1]相等
        }
        if (cnt == n) break; //finished
        m = cnt; //m的可用范围
    }
}

int main () {
    scanf ("%s", s);
    n = strlen (s);
    printf ("n = %d\n", n);
    suffix_sort ();
    for (int i = 1; i <= n; ++i) {
        printf ("%d ", sa[i]);
    }
    return 0;
}

总结流程:

  • 最初 : 推出\(1-sa\)\(1-rk\)

  • 过程 :

    • 根据\(w-sa\)\(w-tp\)

    • 根据\(w-tp\)\(w-rk\)\(2w-sa\)

    • 根据\(2w-sa\)\(w-rk\)\(2w-rk\)

后缀数组(板子)

标签:name   后缀   scanf   turn   过程   临时   scan   eof   space   

原文地址:https://www.cnblogs.com/maomao9173/p/10362172.html

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