most consecutive substrings in a string & longest repeating substring in a string
The string has the most consecutive substring & the longest repeating substring in the string, both of which can be represented by a suffix array, and the suffix array can refer to programming Zhu Ji Nanxiong P156; the suffix array is defined as an array pointer, pointing to the corresponding position in the string, as follows:
A b c A b c a B c D E. substr[0]
bcabcabcde....substr[1]
cabcabcde.......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..---------...
The above substr is the suffix array of abcabcabcde;
First,the most contiguous substring in a stringfor this problem can use the idea of a suffix array, you can see that the substring appears continuously, then meet Substr[0].substr (i, j-i) = Substr[j].substr (0, J-i), know that the program is good to write, Here is the C + + code:
<span style= "FONT-SIZE:18PX;" >string maxtimesofcontinue (String str) {int len = str.length (); int maxCount = 0;string longest = ""; for (int i = 0; i < Len; ++i) {for (int j = i + 1; j < Len; ++j) {if (Str.substr (i, j-i) = = Str.substr (j, j-i)) {int offset = J-i;int Count = 2;for (int k = j + offset; j <= len; k + = offset) {if (Str.substr (i, offset) = = Str.substr (k, offset)) ++count;elsebreak ;} if (Count > MaxCount) {maxCount = Count;longest = Str.substr (i, offset);}}}} return longest;} </span>
second, the longest repeating substring in a string This problem can also be done with the idea of a suffix array, of course, the beginning of the thought is the violent law, that is, all the length of the repeating substring, then choose the longest!
int Comlen (char *str1, char *str2) {int i = 0;while (*str2 && (*str1++ = = *str2++)) ++i;return I;} int MaxLength (char *str) {if (str = = NULL) return 0;int MaxLen = 0;int n = strlen (str); int maxi, maxj;for (int i = 0; i < N ++i) {for (int j = i + 1; j < n; ++j) {int thislen = 0;if ((Thislen = Comlen (&str[i], &str[j])) > MaxLen) {maxle n = Thislen;maxi = I;maxj = J;}}} return maxlen;}
If you use the suffix array method, you can:for string banana, the suffix array isA[0]:banana
A[1]:anana
A[2]:nana
A[3]:ana
A[4]:na
a[5]:a
Sort the suffix array by dictionary
a[0]:a
A[1]:ana
A[2]:anana
A[3]:banana
A[4]:na
A[5]:nana
You can then compare two adjacent substrings:
int Comlen (char *str1, char *str2) {int i = 0;while (*str2 && (*str1++ = = *str2++)) ++i;return I;} INTPSTRCMP (const void *a, const void *b) {return strcmp (* (char**) A, * (char**) b);}//char *a[11];int MaxLength (char *s TR) {if (str = = NULL) return 0;int maxlen = 0;int len = strlen (str); char **a = new char *[len + 1];for (int i = 0; i < len ; ++i) A[i] = &str[i];qsort (A, Len, sizeof (char *), pstrcmp), for (int i = 0; i < len-1; ++i) if (Comlen (A[i], a[i+1]) > maxlen) maxlen = Comlen (A[i], a[i+1]); return maxlen;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Most consecutive substrings in a string & longest repeating substring in a string