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