首页 > 代码库 > luogu P3809 【模板】后缀排序
luogu P3809 【模板】后缀排序
二次联通门 : luogu P3809 【模板】后缀排序
/* luogu P3809 【模板】后缀排序 后缀数组 sa表示 排名为i的是第几个后缀 求出sa数组后输出即可 */ #include <cstdio> #include <cstring> #define Max 1000008 void read (int &now) { register char word = getchar (); for (now = 0; word < ‘0‘ || word > ‘9‘; word = getchar ()); for (; word >= ‘0‘ && word <= ‘9‘; now = now * 10 + word - ‘0‘); } inline void Swap (int *&a, int *&b) { int *now = a; a = b; b = now; } int sa[Max], rank[Max]; int height[Max]; char line[Max]; int str_1[Max], str_2[Max]; int N, M; void Get_Suffix () { register int i; static int c[Max], *x = str_1, *y = str_2; for (i = 0; i < M; c[i ++] = 0); for (i = 0; i < N; c[x[i] = line[i]] ++, i ++); for (i = 1; i < M; c[i] += c[i - 1], i ++); for (i = N - 1; i >= 0; sa[-- c[x[i]]] = i --); register int pos; pos = 1; for (int j = 1; pos < N; j <<= 1, M = pos) { pos = 0; for (i = N - j; i < N; y[pos ++] = i ++); for (i = 0; i < N; i ++) if (sa[i] >= j) y[pos ++] = sa[i] - j; for (i = 0; i < M; c[i ++] = 0); for (i = 0; i < N; c[x[y[i]]] ++, i ++); for (i = 0; i < M; c[i] += c[i - 1], i ++); for (i = N - 1; i >= 0; sa[-- c[x[y[i]]]] = y[i --]); Swap (x, y); pos = 1; x[sa[0]] = 0; for (i = 1; i < N; i ++) x[sa[i]] = y[sa[i - 1]] == y[sa[i]] && y[sa[i - 1] + j] == y[sa[i] + j] ? pos - 1 : pos ++; } } int main (int argc, char *argv[]) { scanf ("%s", line); N = strlen (line); M = 200; Get_Suffix (); for (int i = 0; i < N; i ++) printf ("%d ", sa[i] + 1); return 0; }
luogu P3809 【模板】后缀排序
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。