求字串中重複出現的最長字串

2021-06-10 01:22:02 字數 679 閱讀 1809

求字串中重複出現的最長字串

例如字串:drgabcifrabcsdrrs中,最長公共字串是:abc

方法:利用字尾樹來求。

字串的字尾樹有如下:

drgabcifrabcsdrrs

rgabcifrabcsdrrs

gabcifrabcsdrrs

abcifrabcsdrrs---------s1

rabcsdrrs

abcsdrrs-----------s2

我們求的這些字尾字串中,某2個串的最長公共字首就是想要的答案。比如s1和s2的最長公共字首:abc

我們為了簡化運算,講字尾字串排序,這樣字首相同的就會排在相鄰的位置上。

**如下:

#include#includeusing namespace std;

int commonstr(string str1,string str2)//求兩個字串最長公共字首長度

void maxlen(string str,string &key)//求字串str的最長重複字串,key是用來存放字首的

sort(sub,sub+len);//對字尾字串進行排序

int max = 1;

for(int i = 1;i}

}int main()

字串中重複出現的最長字串 字尾樹思想

字串中重複出現的最長字串maxlenrepeatsubstring.cpp shanghai zilaishui laizi haishanghai output shanghai author arhaiyun date 2013 09 23 include stdafx.h include in...

字串中的最長重複字串

求乙個字串中的最長重複字串 基本思路是利用next陣列來實現 next陣列的定義 就是 字串中第j個字元 必有next j 1個重複 字串,事實上 kmp查詢 求模式串next陣列 就是指求出模式串j個 字元前 最大的重複子串 include include include include defi...

字串問題 求乙個字串中重複出現的最長的子串

2013 09 14 15 34 16 用字尾陣列求乙個字串中重複出現的最長的子串。用c 中的string類可以很方便地進行操作,需將字尾陣列儲存在vector,如下面 中的string版本所示,但這樣就會因為有很大的開銷 直接用字元指標指向字尾字串的首位址,可以節省很大的空間,如下面 中的char...