Advertisement
dimipan80

Advanced Topics 6. Longest Area in Array

Jul 1st, 2014
297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.28 KB | None | 0 0
  1. // 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.
  2.  
  3. namespace _06.LongestAreaInArray
  4. {
  5.     using System;
  6.  
  7.     public class LongestAreaInArray
  8.     {
  9.         public static void Main(string[] args)
  10.         {
  11.             checked
  12.             {
  13.                 int countN;
  14.                 do
  15.                 {
  16.                     Console.Write("Enter a positive Integer number, for Count of All elements: ");
  17.                 }
  18.                 while (!int.TryParse(Console.ReadLine(), out countN) || countN < 1);
  19.  
  20.                 string[] wordSequence = ReadFromInputSequenceOfWordsAndCreateArray(countN);
  21.                 if (wordSequence.Length > 1)
  22.                 {                    
  23.                     int startIndex = 0;
  24.                     int countEqualsElements = 1;
  25.                     int currentIndex = 0;
  26.                     int currentCountEquals = 1;
  27.                     for (int i = 1; i < wordSequence.Length; i++)
  28.                     {
  29.                         string previousWord = wordSequence[i - 1];
  30.                         if (wordSequence[i] == previousWord)
  31.                         {
  32.                             currentCountEquals++;                            
  33.                         }
  34.                         else
  35.                         {
  36.                             if (currentCountEquals > countEqualsElements)
  37.                             {
  38.                                 startIndex = currentIndex;
  39.                                 countEqualsElements = currentCountEquals;
  40.                             }
  41.  
  42.                             currentIndex = i;
  43.                             currentCountEquals = 1;
  44.                         }
  45.                     }
  46.  
  47.                     if (currentCountEquals > countEqualsElements)
  48.                     {
  49.                         startIndex = currentIndex;
  50.                         countEqualsElements = currentCountEquals;
  51.                     }
  52.  
  53.                     PrintLongestAreaInArray(wordSequence, countEqualsElements, startIndex);
  54.                 }
  55.                 else
  56.                 {
  57.                     Console.WriteLine("1\n{0}", wordSequence[0]);
  58.                 }
  59.             }
  60.         }
  61.  
  62.         private static void PrintLongestAreaInArray(string[] words, int countEquals, int start)
  63.         {
  64.             checked
  65.             {
  66.                 Console.WriteLine("The Longest Area In that Sequence is:");
  67.                 Console.WriteLine(countEquals);
  68.                 for (int i = start; i < start + countEquals; i++)
  69.                 {
  70.                     Console.WriteLine(words[i]);
  71.                 }
  72.             }
  73.         }
  74.  
  75.         private static string[] ReadFromInputSequenceOfWordsAndCreateArray(int count)
  76.         {
  77.             checked
  78.             {
  79.                 string[] words = new string[count];
  80.                 for (int i = 0; i < words.Length; i++)
  81.                 {
  82.                     words[i] = Console.ReadLine();
  83.                 }
  84.  
  85.                 return words;
  86.             }
  87.         }
  88.     }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement