Returns the longest repeated substring in the S string.

Source: Internet
Author: User
/** Meaning: * For example, the longest duplicate substring in the string "s =" abcdsetseabcd "is ABCD, and the longest duplicate substring in the string" 4 S = "tmabcdefpiabcdef" is abcdef, the length is 6 S = "aaaaaaaa", the longest duplicate substring is aaaaaaa, And the length is 7 ** analysis: the tool used is the next value used in KMP string matching, the complexity of this algorithm can be reduced to N ^ 2 because of the meaning of the next value: Next [J]! = 0 indicates that a duplicate substring with the length of next [J]-1 exists before the J character. [Note: next calculates the duplicate substring from the first element. * therefore, the basic idea of the algorithm is to find the next value of each substring in string S, the maximum value of next is the longest duplicate substring, and the maximum length is max (next value)-1. Note that if the last character of a string is also one of the duplicate substrings, and it is the largest next, so it is different from other locations, its next does not use-1] [obtain all substrings sub = substring (S, I, strlen (s) -I + 1); I = 1, 2, 3 ...., len-1] * implementation code: int I = 1; int maxl = 0; int maxk = 0; int Pos = 0; // access the starting position of the maximum repeated substring !!! Int Len = strlen (s); While (LEN-I + 1> maxl) {maxk = max {next (substring (S, I, n-I + 1 ))}; // obtain the maxkif (maxk! = Next [Len] | s [Len]! = S [I + maxk-1]) // if the largest duplicate substring contains the last character, the next value is not-1. otherwise, next-1 is the required length {maxk --;} If (maxk> maxl) {maxl = maxk; Pos = I;} I ++ ;}*/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.