Advertisement
Guest User

LongestNonDecreasingSubsequence

a guest
Apr 17th, 2014
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.33 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. class LongestNonDecreasingSubsequence
  6. {
  7.     static int[] SolveInput(string input)
  8.     {
  9.         // read the input and split by space
  10.         string[] numbers = input.Split(' '.ToString().ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
  11.         int[] nums = new int[numbers.Length];
  12.         // save values in array
  13.         for (int i = 0; i < nums.Length; i++)
  14.         {
  15.             nums[i] = int.Parse(numbers[i]);
  16.         }
  17.         return nums;
  18.     }
  19.  
  20.     static List<List<int>> SolvePairs(int[] numbers,ref List<int> numLastIndex)
  21.     {
  22.         // save all posible pairs of non-decreasing elements
  23.         List<List<int>> numSequences = new List<List<int>>();
  24.         for (int i = 0; i < numbers.Length; i++)
  25.         {
  26.             for (int j = i + 1; j < numbers.Length; j++)
  27.             {
  28.                 if (numbers[i] <= numbers[j])
  29.                 {
  30.                     List<int> pairs = new List<int>();
  31.                     pairs.Add(numbers[i]);
  32.                     pairs.Add(numbers[j]);
  33.                     numLastIndex.Add(j);
  34.                     numSequences.Add(pairs);
  35.                 }
  36.             }
  37.         }
  38.         return numSequences;
  39.     }
  40.  
  41.     static void SolveAddMoreDigits(ref List<List<int>> numSequences, List<int> numLastIndex, int[] nums)
  42.     {
  43.         for (int i = 0; i < numLastIndex.Count; i++)
  44.         {
  45.             if (numLastIndex[i] < nums.Length - 2)
  46.             {
  47.                 int count = 1;
  48.                 for (int j = numLastIndex[i + 1]; j < nums.Length; j++)
  49.                 {
  50.                     if (numSequences[i][count] <= nums[j])
  51.                     {
  52.                         numSequences[i].Add(nums[j]);
  53.                         count++;
  54.                     }
  55.                     else if (numSequences[i][count] > nums[j] && numSequences[i][count - 1] <= nums[j])
  56.                     {
  57.                         numSequences[i].RemoveAt(numSequences[i].Count - 1);
  58.                         numSequences[i].Add(nums[j]);
  59.                     }
  60.                 }
  61.             }
  62.         }
  63.     }
  64.  
  65.     static void Main()
  66.     {
  67.         /*
  68.          * Write a program that reads a sequence of integers and finds in it the longest non-decreasing subsequence.
  69.          * In other words, you should remove a minimal number of numbers from the starting sequence,
  70.          * so that the resulting sequence is non-decreasing.
  71.          * In case of several longest non-decreasing sequences, print the leftmost of them.
  72.          * The input and output should consist of a single line, holding integer numbers separated by a space.
  73.          */
  74.  
  75.         string input = Console.ReadLine();
  76.         int[] nums = SolveInput(input);
  77.         List<int> numLastIndex = new List<int>();
  78.         List<List<int>> numSequences = SolvePairs(nums, ref numLastIndex);
  79.  
  80.         SolveAddMoreDigits(ref numSequences, numLastIndex, nums);
  81.         if (numSequences.Count == 0)
  82.         {
  83.             List<int> numSeq = new List<int>();
  84.             numSeq.Add(nums[0]);
  85.             numSequences.Add(numSeq);
  86.         }
  87.  
  88.         var max = numSequences.Max(e=>e.Count);
  89.         var maxSeq = numSequences.Where(e=>e.Count == max).ToList();
  90.         foreach (var item in maxSeq[0])
  91.         {
  92.         Console.Write("{0} ",item);
  93.         }
  94.     }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement