求最長公共字首 字尾陣列 hdu1403

2021-08-22 08:17:13 字數 893 閱讀 8396

【vj鏈結】

給兩個字串,求最長公共子串,多組資料,長度1e5

利用字尾陣列的性質,把兩個串連起來,找最大的滿足公共串部分分別在兩個字串裡的height。

#include 

#include

#include

#include

const

int maxn = 2e5+7;

using namespace std;

///比較兩個串是否相同

///分別比較兩個關鍵字

bool same(int *rank, int idx1, int idx2, int len)

/// 輸入字串的末尾要補乙個 '0' , n是字串的實際長度+1.

char s1[maxn];

int s[maxn];

int sa[maxn],sa2[maxn],rk[maxn],cnt[maxn],hgt[maxn];///rk[i]是i的排名,sa[i]是第i小的開頭

void sa(int s, int n, int m)

///hgt[i]表示第i小和第i-1小的最長公共字首

///h[i]表示i開頭的字尾和它的上一名的最長公共字首,h[i]=hgt[rk[i]];

for(int i=0, j, lcp=0; i1; i++)

}int main()

s[len++]=0;

sa(s,len,27);

int ans=0;

for(i=1;i1],r=sa[i];

if(l>r) swap(l,r);

x=hgt[i];

if(llen1)

}printf("%d\n",ans);

}}

hdu4691(字尾陣列求最長公共字首)

學習鏈結 題意 題目的本質就是求輸入的相鄰的兩個串的最長公共字串,有sa來解求得height陣列,然後用rmq演算法查詢。解題思路 sa模板 st模板 如下 include include include include include include define n 100005 define ...

字尾陣列 LCP(最長公共字首)

sa sa陣列儲存的是乙個1 n的全排列,儲存的是 將所有字尾按字典序排序後,串在原串中的位置。即有suffix sa i suffix sa i 1 rank rank陣列儲存的是 suffix i 在所有字尾中按字典序排序的 名次 總結 字尾陣列是 排第幾的是誰?名次陣列是 你排第幾?lcp i...

字尾陣列之最長公共字首

include define maxn 100 int main s maxn s串可以看成abcbc int i,j,k 0 for i 0 i 5 i rank sa i i for i 0 i 5 i if k k j sa rank i 1 當rank k 不等於0的時候,rank i 1計...