Advertisement
cortez

LongestAreaOfEqualStringsInArray

Oct 6th, 2015
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.22 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace Problem_03__Longest_Area_in_Array
  5. {
  6.     class LongestAreaInArray
  7.     {
  8.         public static void Main ()
  9.         {
  10.             int n;
  11.             string[] stringsArray;
  12.  
  13.             // Checking for errors...
  14.             try
  15.             {
  16.                 n = int.Parse(Console.ReadLine());
  17.                 stringsArray = new string[n];
  18.             }
  19.             catch(Exception e)
  20.             {
  21.                 if(e is FormatException)
  22.                 {
  23.                     Console.WriteLine ("\n--- Format Exception ---\n{0}", e.Message);
  24.                 }
  25.                 else if(e is OverflowException)
  26.                 {
  27.                     Console.WriteLine ("\n--- Overflow Exception ---\n{0}", e.Message);
  28.                 }
  29.                 else
  30.                 {
  31.                     Exception ue = new Exception ("Unknown exception thrown!");
  32.                     Console.WriteLine ("\n--- {0} ---\nPlease try again entering proper integer value!", ue.Message);
  33.                 }
  34.                 return;
  35.             }
  36.  
  37.             // Filling in the array of strings...
  38.             for (int i = 0; i < n; i++)
  39.             {
  40.                 stringsArray [i] = Console.ReadLine ();
  41.             }
  42.  
  43.             // Find the longest equal area in the array of strings...
  44.             List<string> longestArea = FindLongestAreaOfEqualsStrings (stringsArray);
  45.  
  46.             // Print the result on the console...
  47.             PrintStringsOnConsole ("Longest Area in Array of Strings", longestArea);
  48.         }
  49.  
  50.         public static List<string> FindLongestAreaOfEqualsStrings (string[] stringsArray)
  51.         {
  52.             int arrlen = stringsArray.Length;
  53.             int currentSeqLen = 1;
  54.             int bestLen = 1;
  55.             string bestStrSoFar = "";
  56.  
  57.             for (int i = 1; i < arrlen; i++)
  58.             {
  59.                 if(String.Equals(stringsArray[i], stringsArray[i - 1]))
  60.                 {
  61.                     currentSeqLen++;
  62.                     if(currentSeqLen > bestLen)
  63.                     {
  64.                         bestLen = currentSeqLen;
  65.                         bestStrSoFar = stringsArray [i];
  66.                     }
  67.                 }
  68.                 else
  69.                 {
  70.                     currentSeqLen = 1;
  71.                 }
  72.             }
  73.  
  74.             List<string> output = new List<string> ();
  75.             if(bestLen == 1)
  76.             {
  77.                 output.Add(stringsArray [0]);
  78.             }
  79.             else
  80.             {
  81.                 for (int j = 0; j < bestLen; j++)
  82.                 {
  83.                     output.Add(bestStrSoFar);
  84.                 }
  85.             }
  86.             return output;
  87.         }
  88.  
  89.         public static void PrintStringsOnConsole (string printMsg, List<string> longestArea)
  90.         {
  91.             Console.WriteLine ("\n--- {0} ---", printMsg);
  92.             Console.WriteLine (longestArea.Count);
  93.             foreach (string str in longestArea)
  94.             {
  95.                 Console.WriteLine (str);
  96.             }
  97.         }
  98.     }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement