zh_stoqnov

Longest Subsequence

Jul 14th, 2015
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.86 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text.RegularExpressions;
  5.  
  6. namespace Problem_3.Longest_Subsequence
  7. {
  8. class Program
  9. {
  10. static List<int> FindLongestSubsequenceOfEqualIntegers(List<int> inputList)
  11. {
  12. List<int> resultList = new List<int>();
  13. int count = 1,
  14. maxCount = 1,
  15. startIndex = 0,
  16. bestStart = 0;
  17.  
  18. for (int i = 1; i < inputList.Count; i++)
  19. {
  20. int currNum = inputList[i],
  21. prevNum = inputList[i - 1];
  22. if (currNum == prevNum)
  23. {
  24. count++;
  25. startIndex = i - 1;
  26. if (count > maxCount)
  27. {
  28. maxCount = count;
  29. bestStart = startIndex;
  30. }
  31. }
  32. else
  33. {
  34. count = 1;
  35. }
  36. }
  37.  
  38. for (int i = 0; i < maxCount; i++)
  39. {
  40. resultList.Add(inputList[bestStart]);
  41. }
  42. return resultList;
  43. }
  44.  
  45. static void Main(string[] args)
  46. {
  47. var readLine = Console.ReadLine();
  48. if (readLine != null)
  49. {
  50. String input = readLine.Trim();
  51. Regex rgx = new Regex("[0-9\\s]+");
  52. if (!rgx.IsMatch(input))
  53. {
  54. throw new FormatException("Please enter a sequenece of numbers with whitespaces inbetween");
  55. }
  56.  
  57. List<int> numbers = input.Split(' ').Select(int.Parse).ToList();
  58.  
  59. Console.WriteLine(String.Join(" ", FindLongestSubsequenceOfEqualIntegers(numbers)));
  60. }
  61. }
  62. }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment