Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- class LongestArreaInArray
- {
- //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),
- //then find and print the longest sequence of equal elements (first its length, then its elements).
- //If multiple sequences have the same maximal length, print the leftmost of them.
- static void Main()
- {
- int n = int.Parse(Console.ReadLine());
- string[] strings = new string[n];
- for (int i = 0; i < strings.Length; i++)
- {
- strings[i] = Console.ReadLine();
- }
- int countSame = 1;
- int maxCount = 1;
- List<KeyValuePair<int,string>> sames = new List<KeyValuePair<int,string>>();
- for (int i = 0; i < strings.Length - 1; i++)
- {
- while ((i < strings.Length - 1)&&(strings[i] == strings[i+1]))
- {
- countSame++;
- i++;
- }
- sames.Add(new KeyValuePair<int,string>(countSame,strings[i]));
- if (countSame > maxCount)
- {
- maxCount = countSame;
- }
- countSame = 1;
- }
- Console.WriteLine(maxCount);
- for (int i = 0; i < maxCount; i++)
- {
- Console.WriteLine(sames.First(pair => pair.Key == maxCount).Value.ToString());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement