rjangelov

ArraysInPHP - //Problem 2. Largest Sequence of Equal Stri

Jun 25th, 2014
296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.40 KB | None | 0 0
  1. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2. //Problem 2.    Largest Sequence of Equal Strings
  3. //Write a script finds in it the largest sequence of equal elements in array of strings. If several sequences have the same longest //length, print the leftmost of them. The input strings are given as a single line, separated by a space.
  4. <!DOCTYPE html>
  5. <html>
  6.     <head>
  7.         <meta charset="UTF-8">
  8.         <title></title>
  9.     </head>
  10.     <body>
  11.         <form action="LargestSequencePfEqualStrings.php" method="post">
  12.         <input type="text" name="text">
  13.         <input type="submit" value="enter!">
  14.         </form>
  15.     </body>
  16. </html>
  17. <?php
  18. $myString = $_POST['text'];
  19. $myArray=explode(" ", $myString);
  20. $bestNum = 0;
  21. $len = 1;
  22. $bestLen = 1;
  23.  
  24.         $bestNum = $myArray[0];  
  25.         for ($i = 1; $i < count($myArray); $i++)
  26.         {
  27.             if ($myArray[$i - 1] == $myArray[$i])
  28.             {
  29.                 $len++;  
  30.                 if ($len > $bestLen)
  31.                 {
  32.                     $bestLen = $len;
  33.                     $bestNum = $myArray[$i];
  34.                 }
  35.             }
  36.             else $len = 1;    
  37.         }
  38.         echo("The longest sequence is: { ".'<br>');
  39.         for ($i = 0; $i < $bestLen; $i++)
  40.         {
  41.             echo $bestNum.'<br>';
  42.         }
  43.         echo("}");
  44. ?>
Advertisement
Add Comment
Please, Sign In to add comment