日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

洛谷P3809 后缀排序【后缀数组】【模板】

發(fā)布時(shí)間:2025/3/14 编程问答 11 豆豆
生活随笔 收集整理的這篇文章主要介紹了 洛谷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)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。