Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Problem 3. Longest Increasing Sequence
- //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:
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="UTF-8">
- <title></title>
- </head>
- <body>
- <form action="LongestIncreasingSequence.php" method="post">
- <input type="text" name="text">
- <input type="submit" value="enter!">
- </form>
- </body>
- </html>
- <?php
- $myString = $_POST['text'];
- $myArray=explode(" ", $myString);
- for ($i = 0; $i <= count($myArray); $i++)
- {
- if ($myArray[$i-1] < $myArray[$i])
- {
- $stringBuild.=$myArray[$i]." ";
- }
- else{
- $newArr[]=$stringBuild;
- $stringBuild=$myArray[$i]." ";
- }
- }
- echo '<pre>' . print_r($newArr, true) . '</pre>';
- $longest =$newArr[0];
- $maxValue=0;
- for($z=0;$z<=count($newArr);$z++){
- $counter=substr_count($newArr[$z], " ");
- if ($counter>$maxValue) {
- $maxValue=$counter;
- }
- if ($maxValue < substr_count($newArr[$z+1], " ")){
- $longest=$newArr[$z+1];
- }
- }
- echo 'Longest is: '.$longest;
- ?>
Advertisement
Add Comment
Please, Sign In to add comment