GastonFontenla

LIS

Oct 23rd, 2018
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.10 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <vector>
  4.  
  5. using namespace std;
  6.  
  7. vector <int> LIS(vector <int> v)
  8. {
  9.     vector <int> lis(v.size(), 1), padre(v.size(), -1);
  10.     int posMaxLis = 0;
  11.  
  12.     for(int i=0; i<v.size(); i++)
  13.     {
  14.         for(int j=i; j<v.size(); j++)
  15.         {
  16.             if(v[j] > v[i])
  17.             {
  18.                 if(lis[i]+1 > lis[j])
  19.                 {
  20.                     lis[j] = lis[i]+1;
  21.                     padre[j] = i;
  22.                 }
  23.             }
  24.             if(lis[j] > lis[posMaxLis])
  25.             {
  26.                 posMaxLis = j;
  27.             }
  28.         }
  29.     }
  30.  
  31.     ///Hacer backtracking
  32.  
  33.     vector <int> resultado;
  34.  
  35.     do
  36.     {
  37.         resultado.push_back(v[posMaxLis]);
  38.         posMaxLis = padre[posMaxLis];
  39.     }while(posMaxLis >= 0);
  40.  
  41.  
  42.     reverse(resultado.begin(), resultado.end());
  43.  
  44.     return resultado;
  45. }
  46.  
  47. int main()
  48. {
  49.     vector <int> v = {1, 2, 3, 4, 1, 2, 5 ,87, 34, 1, 4, 5, 6, 7};
  50.     vector <int> res = LIS(v);
  51.  
  52.     for(int i=0; i<res.size(); i++)
  53.         cout << res[i] << " ";
  54.     cout << endl;
  55.  
  56.     return 0;
  57. }
Advertisement
Add Comment
Please, Sign In to add comment