PHP calculates consecutive substrings of a repetition
The last time I thought of an algorithm problem, the title is as follows: A string that calculates the duplicate and longest substring. Here is my algorithm, the basic idea is to find a duplicate string, an array, in the array to find the length of each element to form a new array, in splitting the new array, the length of the element and a new array, calculate the maximum value, according to the length of the array each element has the maximum value of the sub-string is calculated.
0) {$newstr. = $str [$i]; $e = strlen ($newstr); $i = $i + $e-1; $oldarray [] = $newstr;}} return $oldarray;} /** * filter out the longest substring * * @param string $str * @return Array */private static function Maxlenth ($STR) {$oldarray = SE Lf::strtoarr ($STR), if (Empty ($oldarray)) {echo "I'm sorry you don't have a duplicate substring"; return false;} $lastarray = Array (), $newlenarray = Array (), foreach ($oldarray as $val) {$newarray [] = $val. '@' . Strlen ($val);} foreach ($newarray as $value) {$numarr = explode (' @ ', $value); $numar [] = $numarr [1];} $maxlength = max ($numar), foreach ($newarray as $valu) {$varray = explode (' @ ', $valu), if (Array_search ($maxlength , $varray)) {$lastarray [] = $varray [0];}} return $lastarray;} /** * Entry File * * @param String $str * @return Array */public static function main ($STR) {$newstr = Self::maxlenth ( $STR); return $newstr;}} $str = "AAAEEEEEBBBBCD"; $newstr = Maxstr::main ($str);p rint_r ($NEWSTR);