Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text.RegularExpressions;
- namespace Problem_3.Longest_Subsequence
- {
- class Program
- {
- static List<int> FindLongestSubsequenceOfEqualIntegers(List<int> inputList)
- {
- List<int> resultList = new List<int>();
- int count = 1,
- maxCount = 1,
- startIndex = 0,
- bestStart = 0;
- for (int i = 1; i < inputList.Count; i++)
- {
- int currNum = inputList[i],
- prevNum = inputList[i - 1];
- if (currNum == prevNum)
- {
- count++;
- startIndex = i - 1;
- if (count > maxCount)
- {
- maxCount = count;
- bestStart = startIndex;
- }
- }
- else
- {
- count = 1;
- }
- }
- for (int i = 0; i < maxCount; i++)
- {
- resultList.Add(inputList[bestStart]);
- }
- return resultList;
- }
- static void Main(string[] args)
- {
- var readLine = Console.ReadLine();
- if (readLine != null)
- {
- String input = readLine.Trim();
- Regex rgx = new Regex("[0-9\\s]+");
- if (!rgx.IsMatch(input))
- {
- throw new FormatException("Please enter a sequenece of numbers with whitespaces inbetween");
- }
- List<int> numbers = input.Split(' ').Select(int.Parse).ToList();
- Console.WriteLine(String.Join(" ", FindLongestSubsequenceOfEqualIntegers(numbers)));
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment