把字符串用后綴樹的形式表現出來如下:
a b c a b c a b c d e .substr[0]
b c a b c a b c d e ....substr[1]
c a b c a b c d e .......substr[2]
a b c a b c d e ..........substr[3]
b c a b c d e .............substr[4]
c a b c d e ...............substr[5]
a b c d e .................substr[6]
b c d e ...................substr[7]
c d e .....................substr[8]
d e ........................substr[9]
e ..........................substr[10]
可以觀察到,若存在連續出現的字串,則滿足 substr[0].substr(i,j-i) == substr[j].substr(0,j-i),例如上例中的
substr[0].substr(0,3-0) == substr[3].substr(0,3-0)
我們換一種方式來看,不需要生成后綴組,但思想還是一樣的。
代碼:
代碼中str.substr(pos2,offset)其實相當於后綴組的substr[pos2].substr(0,offset)
把字符串寫成后綴組其實相當於站在不同的位置往后看這個數組,所以其實並不需要額外增加存儲空間來生成后綴組。
#include <iostream> #include <string> using namespace std; void main(){ string str = "abcabcabcccccdefefefefefef"; int len = str.length(); int maxCount = 0; string longest = ""; for(int pos1 = 0; pos1 < len; pos1++) for(int pos2 = pos1 + 1; pos2 < len; pos2++){ if(str.substr(pos1,pos2-pos1) == str.substr(pos2,pos2-pos1)){ int offset = pos2-pos1; int count = 2; for(int k = pos2 + offset; k <= len; k += offset){ if(str.substr(pos1,offset) == str.substr(k,offset)){ count += 1; }else{ break; } } if(count > maxCount){ maxCount = count; longest = str.substr(pos1,offset); } } } cout << longest << "," << maxCount << endl; }