洛谷P3809 后缀排序【后缀数组】【模板】
生活随笔
收集整理的這篇文章主要介紹了
洛谷P3809 后缀排序【后缀数组】【模板】
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
題目背景
這是一道模板題。
題目描述
讀入一個(gè)長(zhǎng)度為?nn?的由大小寫英文字母或數(shù)字組成的字符串,請(qǐng)把這個(gè)字符串的所有非空后綴按字典序從小到大排序,然后按順序輸出后綴的第一個(gè)字符在原串中的位置。位置編號(hào)為?11?到?nn。
輸入輸出格式
輸入格式:
?
一行一個(gè)長(zhǎng)度為?nn?的僅包含大小寫英文字母或數(shù)字的字符串。
?
輸出格式:
?
一行,共n個(gè)整數(shù),表示答案。
?
輸入輸出樣例
輸入樣例#1:?復(fù)制 ababa 輸出樣例#1:?復(fù)制 5 3 1 4 2說明
n <= 10^6n<=106
?
?
后綴數(shù)組知識(shí)點(diǎn)詳解:
https://www.cnblogs.com/wyboooo/p/9854468.html
1 #include <iostream> 2 #include <set> 3 #include <cmath> 4 #include <stdio.h> 5 #include <cstring> 6 #include <algorithm> 7 #include <vector> 8 #include <queue> 9 #include <map> 10 using namespace std; 11 typedef long long LL; 12 #define inf 0x7f7f7f7f 13 14 const int maxn = 1e6 + 5; 15 char s[maxn]; 16 int n; 17 int sa[maxn]; 18 int t1[maxn], t2[maxn], c[maxn]; 19 int rnk[maxn], height[maxn]; 20 21 void build_sa(char s[], int n, int m) 22 { 23 int i, j, p, *x = t1, *y = t2; 24 for(i = 0; i < m; i++)c[i] = 0; 25 for(i = 0; i < n; i++)c[x[i] = s[i] - '0']++; 26 for(i = 1; i < m; i++)c[i] += c[i - 1]; 27 for(i = n - 1; i >= 0; i--)sa[--c[x[i]]] = i; 28 for(j = 1; j <= n; j <<= 1){ 29 p = 0; 30 for(i = n - j; i < n; i++)y[p++] = i; 31 for(i = 0; i < n; i++)if(sa[i] >= j) y[p++] = sa[i] - j; 32 for(i = 0; i < m; i++)c[i] = 0; 33 for(i = 0; i < n; i++)c[x[y[i]]]++; 34 for(i = 1; i < m; i++)c[i] += c[i - 1]; 35 for(i = n - 1; i >= 0; i--)sa[--c[x[y[i]]]] = y[i]; 36 swap(x, y); 37 p = 1; 38 x[sa[0]] = 0; 39 for(i = 1; i < n; i++) 40 x[sa[i]] = y[sa[i - 1]] == y[sa[i]] && y[sa[i - 1] + j] == y[sa[i] + j] ? p - 1:p++; 41 if(p >= n)break; 42 m = p; 43 } 44 } 45 46 int main() 47 { 48 scanf("%s", s); 49 n = strlen(s); 50 build_sa(s, n, 200); 51 printf("%d", sa[0] + 1); 52 for(int i = 1; i < n; i++){ 53 printf(" %d", sa[i] + 1); 54 } 55 printf("\n"); 56 return 0; 57 }?
轉(zhuǎn)載于:https://www.cnblogs.com/wyboooo/p/9856315.html
總結(jié)
以上是生活随笔為你收集整理的洛谷P3809 后缀排序【后缀数组】【模板】的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 《HTML总结》
- 下一篇: Alpha阶段第2周/共2周 Scrum