The longest repeated string in the string.

Source: Internet
Author: User

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 *}


 

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.