Find the substrings with the most repeated times

Source: Internet
Author: User

Search for the substrings with the most repeated times. We can extend the search for the substrings with the most repeated times by referring to the megabytes.


The basic idea is to record the maximum number of repetitions and record the starting position;


# Include <iostream>

# Include <stdio. h>
Using namespace STD;
Int mystrlen (char * Str)
{
Int sum (0 );
Char * temp = STR;
While (* temp! = '\ 0 ')
{
++ Temp;
++ Sum;
}
Return sum;
}
Bool contain_sub_str (char * STR, char * sub_str)
{
Int len1 = mystrlen (STR );
Int len2 = mystrlen (sub_str );
Int I (0 );
If (len1 <len2)
Return false;
While (I <len2)
{
If (* (STR + I )! = * (Sub_str + I ))
Break;
++ I;
}
If (I = len2)
Return true;
Return false;


}
Int find_max_repeat (char * STR, char * sub_str, int * Len)
{
Int I (0 );
Int num (0), start (0), len2 (mystrlen (sub_str ));
While (* (STR + I )! = '\ 0 ')
{
If (contain_sub_str (STR + I, sub_str )){
Int S = I, K = 0;
While (contain_sub_str (STR + I, sub_str ))
{
I + = len2;
++ K;
}
If (k> num ){
Num = K;
Start = s;
}
}
Else
++ I;
}
* Len = num;
Return start;
}
Int main ()
{
Char STR [] = "abaabaaacdeeabaabaabaabaeefdefdefdsceebd ";
Int I, J;
I = find_max_repeat (STR, "ABA", & J );
Printf ("% d, % d \ n", I, j );
I = find_max_repeat (STR, "EFD", & J );
Printf ("% d, % d \ n", I, j );
Return 0;
}

Find the substrings with the most repeated times

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.