Advertisement
dim4o

Advanced_08_LongestNonDecreasingSubsequenceLow

Apr 18th, 2014
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.55 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace LongestNonDecreasingSubsequence
  6. {
  7.     class SubSeqLow
  8.     {
  9.         static void Main()
  10.         {            
  11.             string input = Console.ReadLine();
  12.             input = input.Trim()
  13.             int[] nums = input.Split(' ').Select(s => int.Parse(s)).ToArray();
  14.             List<List<int>> all = new List<List<int>>();
  15.             List<int> max = new List<int>();
  16.             int currIndex = 0;
  17.             while (currIndex < nums.Length)
  18.             {
  19.                 all.Add(new List<int>());
  20.                 int length = all.Count;
  21.  
  22.                 for (int i = 0; i < length; i++)
  23.                 {
  24.                     List<int> current = new List<int>(all[i]);
  25.                     int lastIndex = current.Count - 1;
  26.                     if (current.Count > 0 && nums[currIndex] < current[lastIndex])
  27.                     {
  28.                         continue;
  29.                     }
  30.                     else
  31.                     {
  32.                         current.Add(nums[currIndex]);
  33.                         all.Add(current);
  34.  
  35.                         if (current.Count > max.Count)
  36.                         {
  37.                             max = current;
  38.                         }
  39.                     }                    
  40.                 }
  41.                 currIndex++;
  42.             }          
  43.             //prints best subsequence
  44.             foreach (var item in max)
  45.             {
  46.                 Console.Write("{0} ", item);
  47.             }
  48.         }
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement