Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace _03.LongestArreainArray
- {
- class LongestAreainArray
- {
- static void Main()
- {
- /* Really hard, I'll try to explain it as best as I can, it took me a while to write this
- I tried looking at other solutions but I couldn't understand them well enough, this is my own version
- I did it in such a way, where the program will find the indexes of the longest sequence of elements, then print
- their length and the elements out.*/
- Console.WriteLine("Enter the number of strings you're going to enter:");
- int numberOfStrings = int.Parse(Console.ReadLine());
- string[] strings = new string[numberOfStrings];
- string indexes = "";
- string highestIndexes = "";
- int currentCount = 0;
- bool hasThisBeenExecuted = false;
- Console.WriteLine("Enter the strings, one at a line.");
- // Assigns the elements to the array
- for (int i = 0; i < numberOfStrings; i++)
- {
- strings[i] = Console.ReadLine();
- }
- //
- for (int k = 0; k < numberOfStrings; k++) // k will be the Array Index we compare to
- {
- for (int i = 1; i < numberOfStrings; i++) // i will be the Array Index that is compared to k
- {
- if (strings[k] == strings[i]) // comparing them
- {
- 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
- if (currentCount == 0) indexes += "" + k; // adds the starter index
- currentCount++; // used to indicate if this is the start of a new comparison
- indexes += " " + i; // adds an index that is equal to 'k'
- hasThisBeenExecuted = true; // i need this for a check if all the elements are different
- }
- else // when the elements stop being equal i.e strings[0] isnt equal to strings[3]
- {
- k = i; // following the example above, this will change k to 3, because we already know 0-3 are all equal
- currentCount = 0; // resets it
- if (indexes.Length > highestIndexes.Length) highestIndexes = indexes; // saves the indexes as the highest
- indexes = ""; // resets it
- }
- }
- }
- if (indexes.Length > highestIndexes.Length) highestIndexes = indexes; /* crucial part of the program,
- if the last elements are equal and longest, the program would not go through "else" to save the indexes as highest,
- so we do one last check here */
- if (hasThisBeenExecuted == false) highestIndexes = "0"; // if no elements have been equal to one another, we take the first one
- List<string> indexesToWrite = highestIndexes.Split(' ').ToList(); // Puts the indexes in a list, removing the spaces in between them
- Console.WriteLine();
- Console.WriteLine(indexesToWrite.Count);
- foreach (string index in indexesToWrite) // Goes through the indexes
- {
- Console.WriteLine(strings[Convert.ToInt32(index)]); // prints out the indexes(elements in the longest sequence)
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement