The longest repeated string in the string.
Note that this is the longest repeated string, as long as there is a repeat, no repeated number of times.
The code below is an implementation given in programming Pearl.
It is implemented through the suffix array.
# Include
# Include
# Include
Using namespace std; int comlen (char * p, char * q) // find the common longest string {int I = 0; if (p = NULL | q = NULL) return 0; while (* p ++ = * q ++) I ++; return I ;} int pstrcmp (const void * a, const void * B) {return strcmp (* (char **) (a), * (char **) (B ));} int main () {char * s = "abcdabce"; char * a [8]; // array with the pointer suffix for (int I = 0; I ++) {if (s [I] = 0) break; a [I] = & s [I]; // pointer suffix array assignment} qsort (a, 8, sizeof (char *), pstrcmp); int maxlen = 0, maxi = 0; for (int I = 0; I <7; I ++) // find the adjacent largest substring if (comlen (a [I], a [I + 1])> maxlen) {maxlen = comlen (a [I], a [I + 1]); maxi = I;} printf ("%. * s \ n ", maxlen, a [maxi]);}
Here, it is particularly noted that the implementation of pstrcmp is not provided in the book, and the bug that occurs during program writing is here.
Int pstrcmp (const void * a, const void * B) {return strcmp (* (char **) (a), * (char **) (B )); // note that the char ** here is not char *}