字尾陣列 模板

2021-07-04 16:30:47 字數 1051 閱讀 7568

const int maxn=500000;

char str[maxn];

int wa[maxn],wb[maxn],wv[maxn],wn[maxn],a[maxn],sa[maxn];

int cmp(int* r,int a,int b,int l)

///n為字串長度,m為字元的取值範圍,r為字串。後面的j為每次排序時子串的長度

void da(int* r,int* sa,int n,int m)

return;

}///字尾陣列 計算height陣列

/**height陣列的值應該是從height[1]開始的,而且height[1]應該是等於0的。

原因是,+因為我們在字串後面新增了乙個0號字元,所以它必然是最小的

乙個字尾。而字串中的其他字元都應該是大於0的(前面有提到,使用倍

增演算法前需要確保這點),所以排名第二的字串和0號字元的公共字首

(即height[1])應當為0.在呼叫calheight函式時,要注意height陣列的範

圍應該是[1..n]。所以呼叫時應該是calheight(r,sa,n)

而不是calheight(r,sa,n+1)。*/

int rank[maxn],height[maxn];

void calheight(int* r,int* sa,int n)

//字尾陣列 倍增演算法 使用方法

/**在使用倍增演算法前,需要保證r陣列的值均大於0。然後要在原字

符串後新增乙個0號字元,具體原因參見羅穗騫的**。這時候,

若原串的長度為n,則實際要進行字尾陣列構建的r陣列的長度應

該為n+1.所以呼叫da函式時,對應的n應為n+1.*/

/* int n=strlen(str);//str 待處理字串

for(int i=0;ia[n]=0;

da(a,sa,n+1,256);

calheight(a,sa,n);*/

//....................................

字尾陣列 模板

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 ...