rjangelov

ArraysInPHP - //Problem 3. Longest Increasing Sequence

Jun 25th, 2014
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.63 KB | None | 0 0
  1. //Problem 3.    Longest Increasing Sequence
  2. //Write a script to find all increasing sequences inside an array of integers. The integers are given in a array. Print the sequences in //the order of their appearance in the input array, each at a single line. Separate the sequence elements by a space. Find also the //longest //increasing sequence and print it at the last line. If several sequences have the same longest length, print the leftmost of //them. //Examples:
  3. <!DOCTYPE html>
  4. <html>
  5.     <head>
  6.         <meta charset="UTF-8">
  7.         <title></title>
  8.     </head>
  9.     <body>
  10.         <form action="LongestIncreasingSequence.php" method="post">
  11.         <input type="text" name="text">
  12.         <input type="submit" value="enter!">
  13.         </form>
  14.     </body>
  15. </html>
  16. <?php
  17. $myString = $_POST['text'];
  18. $myArray=explode(" ", $myString);
  19.         for ($i = 0; $i <= count($myArray); $i++)
  20.         {
  21.             if ($myArray[$i-1] < $myArray[$i])
  22.             {
  23.                 $stringBuild.=$myArray[$i]." ";                
  24.             }
  25.             else{
  26.                 $newArr[]=$stringBuild;
  27.                 $stringBuild=$myArray[$i]." ";
  28.             }
  29.         }
  30.         echo '<pre>' . print_r($newArr, true) . '</pre>';
  31.         $longest =$newArr[0];
  32.         $maxValue=0;
  33.         for($z=0;$z<=count($newArr);$z++){
  34.             $counter=substr_count($newArr[$z], " ");
  35.             if ($counter>$maxValue) {
  36.                 $maxValue=$counter;
  37.             }
  38.             if ($maxValue < substr_count($newArr[$z+1], " ")){
  39.                 $longest=$newArr[$z+1];
  40.             }
  41.         }
  42.         echo 'Longest is: '.$longest;
  43. ?>
Advertisement
Add Comment
Please, Sign In to add comment