【题目15】求字符串的最长重复子串

转载自:http://blog.csdn.net/roynee/archive/2009/07/02/4314791.aspx

 

利用  “后缀数组”(需排序)  求字符串中存在的最长的重复子串,要建立指针数组并通过qsort排序

 时间复杂度是O(nlogn)主要开销在于排序的过程。

#define MAXSIZE 100 #include <iostream> #include <vector> #include <stdio.h> #include <string> using namespace std; string strGiven = ""; /*The Global String*/ int IndexArray[MAXSIZE]; /*The Array Index Array*/ /*** * Just initilize the IndexArray */ void init_strGivenAndIndexArray(string strgiven ) { strGiven = strgiven; for (unsigned int i = 0; i < strgiven.length(); ++i) IndexArray[i] = i; return; } /*** * Output the IndexArray */ void output_OfIndexArray(int len ) { for (int i = 0; i < len; ++i) cout << IndexArray[i] << " "; cout << endl; return; } /*** * Just for qsort args */ inline int compare_OfIndexArray(const void *Index1, const void *Index2 ) { int* index1 = (int *) Index1; int* index2 = (int *) Index2; if ((string)&strGiven[*index1]> (string)&strGiven[*index2]) return 1; //string compare if ((string)&strGiven[*index1] == (string)&strGiven[*index2]) return 0; else return -1; } /*** * make the string to be order * @param strGiven */ inline void enOrder_OfIndexArray() { qsort(IndexArray, strGiven.length(), sizeof(int), compare_OfIndexArray); return; } int getCommonLength(string &comString, string a, string b ) { int i = 0; int k = a.length() < b.length() ? a.length() : b.length(); while (a[i] == b[i] && i < k) ++i; comString = a.substr(0, i); return i; } /*** * [Core Algorithm] Get the max repeat substring * @param strgiven */ void getMaxRepeatSubString(string strgiven ) { vector<string> vecComStr; /*The same len substring, not only one*/ int maxlen = 0; init_strGivenAndIndexArray(strgiven); /*Initilize the global variables*/ //output_OfIndexArray(strgiven.length()); enOrder_OfIndexArray(); /*enorder of array of postfix array of string*/ //output_OfIndexArray(strgiven.length()); for (unsigned int i = 0; i < strgiven.length() - 1; ++i) /* Compare the neighbor*/ { string strTemp = ""; int templen = getCommonLength(strTemp, (string) &strGiven[IndexArray[i]], (string) &strGiven[IndexArray[i + 1]]); if (templen > maxlen) { maxlen = templen; vecComStr.clear(); vecComStr.push_back(strTemp); }else if(templen==maxlen) /*Max len substring not only one*/ vecComStr.push_back(strTemp); } cout << "The max length Repeat SubString of /"" << strgiven << "/"(maxlen is " << maxlen << ") : "; for(vector<string>::iterator it = vecComStr.begin() ; it != vecComStr.end(); ++it) /*for output*/ { if(it!=vecComStr.begin())cout <<" or "; cout <<"/""<<*it<<"/""; } cout<<endl; return; } /*** * The Main Programming * @return */ int main(void ) { getMaxRepeatSubString("abcdabcdefghefgh"); getMaxRepeatSubString("abdddiedddie239000"); getMaxRepeatSubString("XXXXYYYYYYY"); getMaxRepeatSubString("XXXXXXX"); return 0; }

 

你可能感兴趣的:(【题目15】求字符串的最长重复子串)