Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- //Problem 2. Largest Sequence of Equal Strings
- //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.
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="UTF-8">
- <title></title>
- </head>
- <body>
- <form action="LargestSequencePfEqualStrings.php" method="post">
- <input type="text" name="text">
- <input type="submit" value="enter!">
- </form>
- </body>
- </html>
- <?php
- $myString = $_POST['text'];
- $myArray=explode(" ", $myString);
- $bestNum = 0;
- $len = 1;
- $bestLen = 1;
- $bestNum = $myArray[0];
- for ($i = 1; $i < count($myArray); $i++)
- {
- if ($myArray[$i - 1] == $myArray[$i])
- {
- $len++;
- if ($len > $bestLen)
- {
- $bestLen = $len;
- $bestNum = $myArray[$i];
- }
- }
- else $len = 1;
- }
- echo("The longest sequence is: { ".'<br>');
- for ($i = 0; $i < $bestLen; $i++)
- {
- echo $bestNum.'<br>';
- }
- echo("}");
- ?>
Advertisement
Add Comment
Please, Sign In to add comment