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

后缀数组

时间:2015-11-19 18:34:25      阅读:216      评论:0      收藏:0      [点我收藏+]

标签:

uoj模板题:

技术分享
 1 #include <bits/stdc++.h>
 2 #define rep(i, a, b) for (register int i = a; i <= b; i++)
 3 #define drep(i, a, b) for (register int i = a; i >= b; i--)
 4 #define REP(i, a, b) for (int i = a; i < b; i++)
 5 #define pb push_back
 6 #define mp make_pair
 7 #define clr(x) memset(x, 0, sizeof(x))
 8 #define xx first
 9 #define yy second
10 
11 using namespace std;
12 
13 typedef long long i64;
14 typedef pair<int, int> pii;
15 const int inf = ~0U >> 1;
16 const i64 INF = ~0ULL >> 1;
17 //********************************
18 
19 const int maxn = 100005;
20 
21 char s[maxn];
22 int sa[maxn], t[maxn], t2[maxn], c[maxn], n;
23 void build_sa(int m) {
24     int *x = t, *y = t2;
25     REP(i, 0, m) c[i] = 0;
26     REP(i, 0, n) c[x[i] = s[i]]++;
27     REP(i, 1, m) c[i] += c[i - 1];
28     drep(i, n - 1, 0) sa[--c[x[i]]] = i;
29     for (int k = 1; k <= n; k <<= 1) {
30         int p = 0;
31         REP(i, n - k, n) y[p++] = i;
32         REP(i, 0 , n) if (sa[i] >= k) y[p++] = sa[i] - k;
33 
34         REP(i, 0, m) c[i] = 0;
35         REP(i, 0, n) c[x[y[i]]]++;
36         REP(i, 1, m) c[i] += c[i - 1];
37         drep(i, n - 1, 0) sa[--c[x[y[i]]]] = y[i];
38 
39         swap(x, y);
40         p = 1; x[sa[0]] = 0;
41         REP(i, 1, n)
42             x[sa[i]] = y[sa[i - 1]]==y[sa[i]] && y[sa[i - 1] + k]==y[sa[i] + k] ? p - 1 : p++;
43         if (p >= n) break;
44         m = p;
45     }
46 }
47 
48 int rnk[maxn], height[maxn];
49 void getHeight() {
50     REP(i, 0, n) rnk[sa[i]] = i;
51     int k(0);
52     REP(i, 0, n) {
53         if (k) k--;
54         int j = sa[rnk[i] - 1];
55         while (s[i + k] == s[j + k]) k++;
56         height[rnk[i]] = k;
57     }
58 }
59 
60 int main() {
61     scanf("%s", s);
62     n = strlen(s) + 1;
63     build_sa(z + 1);
64     REP(i, 1, n) printf("%d ", sa[i] + 1);
65     puts("");
66     getHeight();
67     REP(i, 2, n) printf("%d ", height[i]);
68     return 0;
69 }
View Code

 

后缀数组

标签:

原文地址:http://www.cnblogs.com/y7070/p/4978282.html

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