Advertisement
Guest User

LongestNonDecreasingSubsequence

a guest
Aug 27th, 2014
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.77 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. class LongestNonDecreasingSubsequence
  7. {
  8.     static void Main(string[] args)
  9.     {
  10.         string input = "1 1 1 3 3 3 2 2 2 2";
  11.         List<int> lnds = input.Split(' ').ToList().Select(x => int.Parse(x)).ToList();
  12.         List<List<int>> activeLists = new List<List<int>>();
  13.         activeLists.Add(new List<int> { lnds.ElementAt(0) });
  14.         int maxLenght = 0;
  15.  
  16.         for (int currentElement = 1; currentElement < lnds.Count; currentElement++)
  17.         {
  18.             int c = lnds.ElementAt(currentElement);
  19.  
  20.             bool isSmallest = true;
  21.             int count = activeLists.Count;
  22.             for (int i = 0; i < count; i++)
  23.             {
  24.                 if (c >= activeLists.ElementAt(i).Last())
  25.                 {
  26.                     List<int> temp = CloneList(activeLists.ElementAt(i));
  27.                     temp.Add(c);
  28.                     activeLists.Add(temp);
  29.                     isSmallest = false;
  30.                     maxLenght = (activeLists.Last().Count > maxLenght) ? activeLists.Last().Count : maxLenght;
  31.                 }
  32.             }
  33.  
  34.             if (isSmallest)
  35.             {
  36.                 activeLists.Add(new List<int> { c });
  37.                 continue;
  38.             }
  39.         }
  40.         foreach (List<int> x in activeLists)
  41.         {
  42.             if (x.Count == maxLenght)
  43.             {
  44.                 foreach (int a in x)
  45.                     Console.Write("{0} ", a);
  46.                 Console.WriteLine();
  47.                 break;
  48.             }
  49.         }
  50.     }
  51.  
  52.  
  53.     public static List<int> CloneList(List<int> l)
  54.     {
  55.         List<int> newList = new List<int>();
  56.         l.ForEach(item => newList.Add(item));
  57.         return newList;
  58.     }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement