Advertisement
Guest User

areaInArray

a guest
Apr 9th, 2014
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.26 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace _6_LongestAreaInArray
  5. {
  6.     class LongestAreaInArray
  7.     {
  8.         public static List<string> FindTheLongestSequance(IList<string> elements)
  9.         {
  10.  
  11.             if (elements != null && elements.Count > 0)
  12.             {
  13.                 int maxSequance = 1;
  14.                 int currentSequance = 1;
  15.                 string elementOfTheLongestSequance = elements[0];
  16.                 for (int i = 1; i <elements.Count; i++)
  17.                 {
  18.                    
  19.                     if (elements[i] == elements[i - 1])
  20.                     {
  21.                         currentSequance++;
  22.                     }
  23.                     else
  24.                     {
  25.                         if (maxSequance < currentSequance)
  26.                         {
  27.                             elementOfTheLongestSequance = elements[i - 1];
  28.                             maxSequance = currentSequance;
  29.                         }
  30.                         currentSequance = 1;
  31.                     }
  32.                    
  33.                 }
  34.                 if (maxSequance < currentSequance)
  35.                 {
  36.                     elementOfTheLongestSequance = elements[elements.Count-1];
  37.                     maxSequance = currentSequance;
  38.                 }
  39.  
  40.                 List<string> sequance = new List<string>();
  41.                 for (int i = 0; i < maxSequance; i++)
  42.                 {
  43.                     sequance.Add(elementOfTheLongestSequance);
  44.                 }
  45.                 return sequance;
  46.             }
  47.             else
  48.             {
  49.                 throw new ArgumentNullException("The collection is empty or not initialized!");
  50.             }
  51.         }
  52.  
  53.         static void Main(string[] args)
  54.         {
  55.             int n = int.Parse(Console.ReadLine());
  56.             List<string> elements=new List<string>();
  57.  
  58.             for (int i = 0; i < n; i++)
  59.             {
  60.                 elements.Add(Console.ReadLine());
  61.             }
  62.  
  63.             List<string> longestSequance = FindTheLongestSequance(elements);
  64.  
  65.             Console.WriteLine(longestSequance.Count);
  66.             foreach (var element in longestSequance)
  67.             {
  68.                 Console.WriteLine(element);
  69.             }
  70.         }
  71.     }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement