标签:排列 max 字典序 void top height return red bool
P1860 后缀数组
aabaaaab
4 5 6 1 7 2 8 3 0 3 2 3 1 2 0 1
后缀数组模板
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn = 200000 + 10; int n, m; char s[maxn]; int sa[maxn], rank[maxn], height[maxn], tax[maxn], tp[maxn]; inline bool cmp(int *arr, int x, int y, int l){ return arr[x] == arr[y] && arr[x + l] == arr[y + l]; } inline void Rsort(){ for(int i = 0; i <= m; i++) tax[i] = 0; for(int i = 1; i <= n; i++) tax[rank[tp[i]]]++; for(int i = 1; i <= m; i++) tax[i] += tax[i - 1]; for(int i = n; i; i--) sa[tax[rank[tp[i]]]--] = tp[i]; } void suffix(){ m = 127; for(int i = 1; i <= n; i++){ rank[i] = s[i]; tp[i] = i; } Rsort(); for(int w = 1, p; w < n; m = p, w <<= 1){ p = 0; for(int i = n - w + 1; i <= n; i++) tp[++p] = i; for(int i = 1; i <= n; i++) if(sa[i] > w) tp[++p] = sa[i] - w; Rsort(); swap(rank, tp); p = rank[sa[1]] = 1; for(int i = 2; i <= n; i++) rank[sa[i]] = cmp(tp, sa[i - 1], sa[i], w) ? p : ++p; } for(int i = 1, j, k = 0; i <= n; height[rank[i++]] = k) for(k ? k-- : 0, j = sa[rank[i] - 1]; s[i + k] == s[j + k]; k++); } int main(){ scanf("%s", s + 1); n = strlen(s + 1); suffix(); for(int i = 1; i <= n; i++) printf("%d ", sa[i]); puts(""); for(int i = 1; i <= n; i++) printf("%d ", height[i]); return 0; }
标签:排列 max 字典序 void top height return red bool
原文地址:http://www.cnblogs.com/ruoruoruo/p/7516722.html