Advertisement
Karlie

C#Homework7_LongestAreaInArray

Apr 24th, 2014
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. class LongestArreaInArray
  6. {
  7. //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),
  8. //then find and print the longest sequence of equal elements (first its length, then its elements).
  9. //If multiple sequences have the same maximal length, print the leftmost of them.
  10. static void Main()
  11. {
  12. int n = int.Parse(Console.ReadLine());
  13. string[] strings = new string[n];
  14.  
  15. for (int i = 0; i < strings.Length; i++)
  16. {
  17. strings[i] = Console.ReadLine();
  18. }
  19.  
  20. int countSame = 1;
  21. int maxCount = 1;
  22. List<KeyValuePair<int,string>> sames = new List<KeyValuePair<int,string>>();
  23.  
  24. for (int i = 0; i < strings.Length - 1; i++)
  25. {
  26. while ((i < strings.Length - 1)&&(strings[i] == strings[i+1]))
  27. {
  28. countSame++;
  29. i++;
  30. }
  31. sames.Add(new KeyValuePair<int,string>(countSame,strings[i]));
  32. if (countSame > maxCount)
  33. {
  34. maxCount = countSame;
  35. }
  36. countSame = 1;
  37. }
  38.  
  39. Console.WriteLine(maxCount);
  40. for (int i = 0; i < maxCount; i++)
  41. {
  42. Console.WriteLine(sames.First(pair => pair.Key == maxCount).Value.ToString());
  43. }
  44. }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement