PHP calculates the continuation of a repeating substring

Source: Internet
Author: User
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);


  • 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.