Advertisement
Guest User

Longest Area in Array

a guest
Sep 29th, 2015
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.79 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace _03.LongestArreainArray
  6. {
  7.     class LongestAreainArray
  8.     {
  9.         static void Main()
  10.         {
  11.             /* Really hard, I'll try to explain it as best as I can, it took me a while to write this
  12.             I tried looking at other solutions but I couldn't understand them well enough, this is my own version
  13.            
  14.             I did it in such a way, where the program will find the indexes of the longest sequence of elements, then print
  15.             their length and the elements out.*/
  16.            
  17.             Console.WriteLine("Enter the number of strings you're going to enter:");
  18.             int numberOfStrings = int.Parse(Console.ReadLine());
  19.             string[] strings = new string[numberOfStrings];
  20.  
  21.             string indexes = "";
  22.             string highestIndexes = "";
  23.             int currentCount = 0;    
  24.             bool hasThisBeenExecuted = false;
  25.             Console.WriteLine("Enter the strings, one at a line.");
  26.  
  27.             // Assigns the elements to the array
  28.             for (int i = 0; i < numberOfStrings; i++)
  29.             {
  30.                 strings[i] = Console.ReadLine();
  31.             }
  32.             //
  33.  
  34.             for (int k = 0; k < numberOfStrings; k++) // k will be the Array Index we compare to
  35.             {
  36.                 for (int i = 1; i < numberOfStrings; i++) // i will be the Array Index that is compared to k
  37.                 {                  
  38.                         if (strings[k] == strings[i]) // comparing them
  39.                         {
  40.                             if (i  == numberOfStrings-1) k = i; // checks if this is the last iteration of the loop, if it is, it will stop after this iteration
  41.                             if (currentCount == 0) indexes += "" + k; // adds the starter index
  42.                             currentCount++;                            // used to indicate if this is the start of a new comparison
  43.                             indexes += " " + i;                        // adds an index that is equal to 'k'
  44.                             hasThisBeenExecuted = true;                // i need this for a check if all the elements are different
  45.                         }
  46.                         else // when the elements stop being equal i.e strings[0] isnt equal to strings[3]
  47.                         {
  48.                             k = i;       // following the example above, this will change k to 3, because we already know 0-3 are all equal
  49.                             currentCount = 0;             // resets it
  50.  
  51.                             if (indexes.Length > highestIndexes.Length) highestIndexes = indexes; // saves the indexes as the highest
  52.                             indexes = "";           // resets it
  53.                         }
  54.                    
  55.                 }              
  56.           }
  57.             if (indexes.Length > highestIndexes.Length) highestIndexes = indexes; /* crucial part of the program,
  58.             if the last elements are equal and longest, the program would not go through "else" to save the indexes as highest,
  59.             so we do one last check here */
  60.  
  61.             if (hasThisBeenExecuted == false) highestIndexes = "0"; // if no elements have been equal to one another, we take the first one
  62.                      
  63.             List<string> indexesToWrite = highestIndexes.Split(' ').ToList(); // Puts the indexes in a list, removing the spaces in between them
  64.  
  65.             Console.WriteLine();
  66.             Console.WriteLine(indexesToWrite.Count);
  67.             foreach (string index in indexesToWrite) // Goes through the indexes
  68.             {
  69.                 Console.WriteLine(strings[Convert.ToInt32(index)]); // prints out the indexes(elements in the longest sequence)
  70.             }
  71.         }
  72.     }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement