Advertisement
Ludmil

[C# basic] Pr6. * Longest Non-Decreasing Sequence

Apr 17th, 2014
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.35 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. /*Problem 6.    * Longest Non-Decreasing Sequence
  6. Write a program that reads a sequence of integers and finds in it the longest non-decreasing subsequence. In other words, you should remove a minimal number of numbers from the starting sequence, so that the resulting sequence is non-decreasing. In case of several longest non-decreasing sequences, print the leftmost of them. The input and output should consist of a single line, holding integer numbers separated by a space.
  7. */
  8. /*https://www.youtube.com/watch?v=Sk0PX0YSHtk */
  9. class Program
  10. {
  11.     static void Main()
  12.     {
  13.         string numberStr = Console.ReadLine();// enter numers on one line separated by space
  14.         char[] split = new char[] { ' ', ',', ';' };//split by
  15.         string[] spliedNum = numberStr.Split(split, StringSplitOptions.RemoveEmptyEntries);
  16.         int[] numbers = new int[spliedNum.Length];
  17.         int n = numbers.Length;// how many numbers we have
  18.         for (int i = 0; i < n; i++)
  19.         {
  20.             if (int.TryParse(spliedNum[i], out numbers[i])) numbers[i] = int.Parse(spliedNum[i]);
  21.         }
  22.         List<int> numberSeq = new List<int>();
  23.         List<int> numberSeqMax = new List<int>();
  24.         int maxI = (int)Math.Pow((double)2, n) - 1; // number of combination needed to generate  using number as binary
  25.         for (int i = maxI; i >= 1; i--)// no need to check for zeroes start from last to first fo first true and stop
  26.         {// combination of numbers we will take or not
  27.             string numBinStr = Convert.ToString(i, 2).PadLeft(n, '0');// generate combination
  28.             for (int bit = 0; bit < numBinStr.Length; bit++)
  29.             {// generate unique List of sequnece combination
  30.                 if (numBinStr[bit] == '1')
  31.                 {
  32.                     numberSeq.Add(numbers[bit]);
  33.                 }
  34.             }
  35.             bool isNonDecr = true;
  36.             List<int> countEncr = new List<int>();
  37.             List<int> countEqual = new List<int>();
  38.             for (int y = 0; y < numberSeq.Count - 1; y++)
  39.             { // serch for Encreasing
  40.                 if (numberSeq[y] > numberSeq[y + 1])
  41.                 {// check and skip search for increasing/ equals
  42.                     isNonDecr = false;
  43.                 }
  44.             }
  45.             if (isNonDecr)
  46.             {
  47.                 for (int y = 0; y < numberSeq.Count - 1; y++)
  48.                 { // serch for equal
  49.                     if (numberSeq[y] == numberSeq[y + 1])
  50.                     {//1 1 1 2 2 2 - > 1 1 1 or 4 1 4 2 4 3 4 - > 4 4 4 4
  51.                         countEqual.Add(numberSeq[y]);
  52.                         if (y == numberSeq.Count - 2)// chech before before the last // this if stay inside
  53.                         {// add last elm
  54.                             countEqual.Add(numberSeq[y + 1]);
  55.                         }
  56.                     }
  57.                     else break;
  58.                 }
  59.                 for (int y = 0; y < numberSeq.Count - 1; y++)
  60.                 { // serch for Encreasing
  61.                     if (numberSeq[y] < numberSeq[y + 1])
  62.                     { // 4 1 1 2  - > 1 2
  63.                         countEncr.Add(numberSeq[y]);
  64.                         if (y == numberSeq.Count - 2)// chech before before the last // this if stay inside
  65.                         {// add last elm
  66.                             countEncr.Add(numberSeq[y + 1]);
  67.                         }
  68.                     }
  69.                     else break;
  70.                 }
  71.             }
  72.  
  73.             if (isNonDecr && countEncr.Count > numberSeqMax.Count)
  74.             {// add countEncr // 4 1 1 2  - > 1 2  
  75.                 numberSeqMax.Clear();
  76.                 numberSeqMax = countEncr.ToList();
  77.                 numberSeq.Clear();
  78.                 countEncr.Clear();
  79.             }
  80.             else if (isNonDecr && countEqual.Count > numberSeqMax.Count)
  81.             {//add countEqual //1 1 1 2 2 2 - > 1 1 1 /// 1 4 1  2 1  - > 1 1 1
  82.                 numberSeqMax.Clear();
  83.                 numberSeqMax = countEqual.ToList();
  84.                 numberSeq.Clear();
  85.                 countEqual.Clear();
  86.             }
  87.             else numberSeq.Clear();//clear sequence
  88.         }
  89.         foreach (var item in numberSeqMax)
  90.         {
  91.             Console.WriteLine(item);
  92.         }
  93.     }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement