Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Write a program to find the longest area of equal elements in array of strings. You first should read an integer n and n strings (each at a separate line), then find and print the longest sequence of equal elements (first its length, then its elements). If multiple sequences have the same maximal length, print the leftmost of them.
- namespace _06.LongestAreaInArray
- {
- using System;
- public class LongestAreaInArray
- {
- public static void Main(string[] args)
- {
- checked
- {
- int countN;
- do
- {
- Console.Write("Enter a positive Integer number, for Count of All elements: ");
- }
- while (!int.TryParse(Console.ReadLine(), out countN) || countN < 1);
- string[] wordSequence = ReadFromInputSequenceOfWordsAndCreateArray(countN);
- if (wordSequence.Length > 1)
- {
- int startIndex = 0;
- int countEqualsElements = 1;
- int currentIndex = 0;
- int currentCountEquals = 1;
- for (int i = 1; i < wordSequence.Length; i++)
- {
- string previousWord = wordSequence[i - 1];
- if (wordSequence[i] == previousWord)
- {
- currentCountEquals++;
- }
- else
- {
- if (currentCountEquals > countEqualsElements)
- {
- startIndex = currentIndex;
- countEqualsElements = currentCountEquals;
- }
- currentIndex = i;
- currentCountEquals = 1;
- }
- }
- if (currentCountEquals > countEqualsElements)
- {
- startIndex = currentIndex;
- countEqualsElements = currentCountEquals;
- }
- PrintLongestAreaInArray(wordSequence, countEqualsElements, startIndex);
- }
- else
- {
- Console.WriteLine("1\n{0}", wordSequence[0]);
- }
- }
- }
- private static void PrintLongestAreaInArray(string[] words, int countEquals, int start)
- {
- checked
- {
- Console.WriteLine("The Longest Area In that Sequence is:");
- Console.WriteLine(countEquals);
- for (int i = start; i < start + countEquals; i++)
- {
- Console.WriteLine(words[i]);
- }
- }
- }
- private static string[] ReadFromInputSequenceOfWordsAndCreateArray(int count)
- {
- checked
- {
- string[] words = new string[count];
- for (int i = 0; i < words.Length; i++)
- {
- words[i] = Console.ReadLine();
- }
- return words;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement