字尾陣列入門,字尾陣列模板整理

2021-09-26 11:00:46 字數 2206 閱讀 4078

我自己懶得寫,就是想寫個部落格儲存下大佬的部落格位址

點這模板題

大佬的模板:

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#define inf 0x3f3f3f3f

#define ll long long

using

namespace std;

const

int maxn =

500;

int n,w;

char str[maxn]

;int sa[maxn]

;int buc[maxn]

;int x[maxn]

,y[maxn]

,len;

int rank[maxn]

,height[maxn]

;///rank[i] 用來記錄字尾 i 在 sa 陣列的位置,表示字串i的排名

///height[i] 排名為 i 的字尾與排名為 i-1 的字尾的 lcp(最長公共字首)

int h[maxn]

;//h[i] = height[rank[i]]:排名為i的字尾與排名靠前一位的最長公共字首 ??????????????

//rank:這個排在第幾,sa:排在第幾的是誰

voidda(

)if(p >= len)

break

; m = p;}}

//如果直接求的話,複雜度為o(n^2),但是我們按照h[1],h[2],h[3]....的順序求解,並利用h陣列的性質,時間複雜度可以降為o(n)

//h[i] >= h[i-1]+1 h[i] = height[rank[i]]

void

get_height()

// 第乙個字尾的 lcp 為 0。

if(k) k--

;// 從 k - 1 開始推

int j = sa[rank[i]-1

];while

(str[i + k]

== str[j + k]

&& i + k < len && j + k < len) k++

; height[rank[i]

]= k;}}

intmain()

}

#include

#include

#include

#include

#define inf 0x3f3f3f3f

#define ll long long

using

namespace std;

const

int maxn =

1e6+5;

int n,w;

char str[maxn]

;int sa[maxn]

;int buc[maxn]

;int x[maxn]

,y[maxn]

,len;

int rank[maxn]

,height[maxn]

;///height[i] 排名為 i 的字尾與排名為 i-1 的字尾的 lcp(最長公共字首)

int h[maxn]

;//h[i] = height[rank[i]]:排名為i的字尾與排名靠前一位的最長公共字首 ??????

voidda(

)if(p >= len)

break

; m = p;}}

void

get_height()

// 第乙個字尾的 lcp 為 0。

if(k)

k--;// 從 k - 1 開始推

int j = sa[rank[i]-1

];while

(str[i + k]

== str[j + k]

&& i + k < len && j + k < len)k++

; height[rank[i]

]= k;}}

intmain()

字尾陣列 模板

char s n 陣列的長度要為兩倍的 int n n全域性變數為字元陣列的長度的 int sa n 2 high n 2 rank n 2 tmp n 2 top n 2 void makesa void lcp int main gets s int len strlen s s len get...

字尾陣列模板

過了期末了,繼續寫acm題 自己寫的字尾陣列模板。k,len,rank,sa,tmp,都要寫在外面,這樣就不用來回折騰了。也是使用倍增法來做的,得到乙個sa,儲存了字尾排在第i位的字尾的起始位置。因為空也算乙個字尾,所以函式中都是 len include include include includ...

模板 字尾陣列

include include includeusing namespace std const int max 20001 int num max int sa max rank max height max int wa max wb max wv max wd max int cmp int ...