求兩個字串的最長公共子串。
將兩個字串連線為乙個新字串,並計算字尾陣列和高度陣列lcp。
然後檢查字尾陣列中所有相鄰的字尾,其中字尾分別屬於第一和第二個字串的lcp的最大值就是答案。
#include #include #include #include #include #include #include #include #include #include using namespace std;
#define maxn 200005
int r[maxn];
int rank[maxn],height[maxn],sa[maxn];
//sa[1~n]為有效值,sa[0]必定是n
//height[2~n]為有效值,height[i]代表是s[sa[i-1]...]和s[sa[i]...]的最長公公字首lcp
int t1[maxn],t2[maxn],c[maxn];//輔助陣列
bool cmp(int *r, int a, int b, int l)
//logn次基數排序,o(nlogn)
void da(int *str,int n,int m) //n為字串長度,m為字元最大值
n--;
int k=0;
for(i=0; i<=n; i++) rank[sa[i]]=i;
for(i=0; i
poj 2774 字尾陣列
include include define debug ifdef debug define debug printf va args else define debug endif define maxn 200004 define min a,b a b a b int wx maxn wy ...
POJ 2774 字尾陣列
題意 求兩個串的最長公共子串 思路 在求出height陣列之後,再把sa陣列區分出來,只要其中乙個sa i s i 1 陣列是屬於第一串,s i 1 s i 屬於第二串,那麼我們可以求得其最大值,之所以可以這樣做,是因為sa陣列已經對字串按字典序排好序了 include include includ...
POJ 2774 字尾陣列基礎題
poj 2774 題意 求兩個字串的最長公共子串。總結 搞了半天還是不太理解,看著大神部落格強行敲的。而且還看到有hash 二分做的。poj 2774 include include include include include include include include include in...