Advertisement
Guest User

Untitled

a guest
Jul 13th, 2014
661
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.01 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. // Please note that the examples in the homework.doc file are false (some of them).
  8. // For example: in the sequence 1 1 1 2 2 2 2, the longest non-decreasing subsequence is  1 1 1 2 2 2 2 not 2 2 2 2.
  9. // It's logical isn't it? It's a longest non-decreasing subsequence
  10. // and not a longest non-decreasing subsequence with equal elements or something like that.
  11. // Furthermore, the output of the last example (11 12 13 3 14 4 15 5 6 7 8 7 16 9 8) is false again, because it prints out
  12. // 3 4 5 6 7 8 9 and not 3 4 5 6 7 8 16. The second output is the correct one because in the task it is said:
  13. // "In case of several longest non-decreasing sequences, print the leftmost of them"
  14.  
  15. // Anyways, for this example I have used recursion in order to solve it. You can read more about it here:
  16. // http://www.introprogramming.info/intro-csharp-book/read-online/glava10-rekursia/
  17.  
  18. class LongestNonDecreasingSubsequence
  19. {
  20.     static int numberOfLoops;
  21.     static int countOfNumbers;
  22.     static int[] loops;
  23.     static int[] numbers;
  24.     static int[] longestPositiveSequence = new int[0];
  25.  
  26.     static void Main()
  27.     {
  28.         Console.Write("Enter numbers:");
  29.         string[] input = Console.ReadLine().Split();
  30.  
  31.         countOfNumbers = input.Length;
  32.         numberOfLoops = countOfNumbers;
  33.         numbers = new int[countOfNumbers];
  34.  
  35.         for (int index = 0; index < input.Length; index++)
  36.         {
  37.             numbers[index] = Convert.ToInt32(input[index]);
  38.         }
  39.  
  40.         while (numberOfLoops > 0)
  41.         {
  42.             loops = new int[numberOfLoops];
  43.             FindSubsets(0, 0);
  44.             numberOfLoops--;
  45.         }
  46.  
  47.         Console.WriteLine();
  48.         foreach (var item in longestPositiveSequence)
  49.         {
  50.             Console.Write(item + " ");
  51.         }
  52.     }
  53.  
  54.     static void FindSubsets(int currentLoop, int lastNumber)
  55.     {
  56.         if (currentLoop == numberOfLoops)
  57.         {
  58.             CheckSequence();
  59.             return;
  60.         }
  61.  
  62.         for (int nextNumber = lastNumber; nextNumber < countOfNumbers; nextNumber++)
  63.         {
  64.             loops[currentLoop] = numbers[nextNumber];
  65.             FindSubsets(currentLoop + 1, nextNumber + 1);
  66.         }
  67.     }
  68.  
  69.     private static void CheckSequence()
  70.     {
  71.         bool positiveSequence = true;
  72.  
  73.         for (int index = 0; index < loops.Length - 1; index++)
  74.         {
  75.             if (loops[index] > loops[index + 1])
  76.             {
  77.                 positiveSequence = false;
  78.                 break;
  79.             }
  80.         }
  81.  
  82.         if (positiveSequence)
  83.         {
  84.             if (loops.Length > longestPositiveSequence.Length)
  85.             {
  86.                 longestPositiveSequence = new int[loops.Length];
  87.                 for (int index = 0; index < loops.Length; index++)
  88.                 {
  89.                     longestPositiveSequence[index] = loops[index];
  90.                 }
  91.             }
  92.         }
  93.     }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement