Advertisement
VladoG

[List] - Homework - Largest Increasing Subsequence

Apr 15th, 2016
804
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.79 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. namespace _13_Largest_Increasing_Subsequence
  8.     {
  9.     class LargestIncreasingSubsequence
  10.         {
  11.         static void Main(string[] args)
  12.             {
  13.             // InputList - for TEST ONLY
  14.             // string inputStr = "1";
  15.             // string inputStr = "7 3 5 8 -1 0 6 7";
  16.             // string inputStr = "1 2 5 3 5 2 4 1";
  17.             // string inputStr = "0 10 20 30 30 40 1 50 2 3 4 5 6";
  18.             // string inputStr = "11 12 13 3 14 4 15 5 6 7 8 7 16 9 8";
  19.  
  20.             string inputStr = Console.ReadLine();
  21.             List<int> inputL = inputStr.Split(new string[] {" "},StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToList();
  22.             int countSeq = 0;
  23.             int len = inputL.Count;
  24.             int maxL = inputL.Max();
  25.             int nextN = 1;
  26.             int tempPos = 1;
  27.             int pos;
  28.             List<int> tempL = new List<int>();
  29.             List<int> posIncSubseqL = new List<int>();
  30.  
  31.             //Console.WriteLine(string.Join(" ", inputL));
  32.             //Console.WriteLine($"len = {len} --- InputL - MAXelement: {maxL}\n");
  33.  
  34.             // Search for Subsequences
  35.             if (len == 1)
  36.                 {
  37.                 tempL.Add(inputL[0]);
  38.                 tempPos = 0;
  39.                 countSeq = 1;
  40.                 }
  41.             else
  42.                 {
  43.                 for (int i = 0; i < len - 1; i++)
  44.                     {
  45.                     tempL.Add(inputL[i]);
  46.                     tempPos = i + 1;
  47.                     do
  48.                         {
  49.                         for (int j = tempPos; j < len; j++)
  50.                             {
  51.                             if (inputL[j] == inputL[i] + nextN)
  52.                                 {
  53.                                 tempL.Add(inputL[j]);
  54.                                 // Console.WriteLine($"Finded NEXT Num --> {inputL[j]} --- Pos = {j}");
  55.                                 tempPos = j + 1;
  56.                                 break;
  57.                                 }
  58.                             if ((inputL[i] + nextN) > maxL)
  59.                                 {
  60.                                 // Console.WriteLine($"BREAK at POS --> {j}");
  61.                                 break;
  62.                                 }
  63.                             }
  64.                         nextN++;
  65.                         } while (nextN <= maxL);
  66.                     posIncSubseqL.Add(i); // Record START POSition of current Subsequence
  67.                     posIncSubseqL.Add(tempL.Count); // Record LENgth of current Subsequence
  68.  
  69.                     //Console.WriteLine("tempL:");
  70.                     //Console.WriteLine(string.Join(" ", tempL));
  71.                     //Console.WriteLine($"LENGTH tempL: {tempL.Count}");
  72.                     //Console.WriteLine("CLEAR tempL !!!");
  73.  
  74.                     tempL.Clear(); // CLEAR tempL
  75.                     nextN = 1; // CLEAR nextN
  76.  
  77.                     } // End of FOR-i
  78.  
  79.                 //Console.WriteLine("posIncSubseqL:");
  80.                 //Console.WriteLine(string.Join(" ", posIncSubseqL));
  81.  
  82.                 for (int i = 1; i < posIncSubseqL.Count; i += 2)
  83.                     {
  84.                     if (posIncSubseqL[i] > countSeq)
  85.                         {
  86.                         countSeq = posIncSubseqL[i];
  87.                         tempPos = posIncSubseqL[i - 1];
  88.                         }
  89.                     }
  90.                 } // End ELSE
  91.             // Console.WriteLine($"\n MAX LENGTH: {countSeq} at POSition: {tempPos}");
  92.  
  93.             // Print Largest Increasing Subsequence (If several - print the LEFTmost)
  94.             // Console.WriteLine($"POSITION if 1 ----> {tempPos}");
  95.             pos = tempPos;
  96.             tempL.Clear();
  97.             tempL.Add(inputL[pos]);
  98.             tempPos = pos + 1;
  99.             nextN = 1;
  100.  
  101.             if (countSeq > 1)
  102.                 {
  103.                 do
  104.                     {
  105.                     for (int j = tempPos; j < len; j++)
  106.                         {
  107.                         if (inputL[j] == inputL[pos] + nextN)
  108.                             {
  109.                             tempL.Add(inputL[j]);
  110.                             // Console.WriteLine($"Finded NEXT Num --> {inputL[j]} --- Pos = {j}");
  111.                             tempPos = j + 1;
  112.                             break;
  113.                             }
  114.                         }
  115.                     nextN++;
  116.                     } while (nextN <= maxL);
  117.                 }
  118.  
  119.             // Console.WriteLine("FINAL Subsequence:");
  120.  
  121.             Console.WriteLine(string.Join(" ", tempL));
  122.            
  123.             // Console.WriteLine($"LENGTH tempL: {tempL.Count}");
  124.  
  125.  
  126.             } // End of Main
  127.         }
  128.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement