Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <algorithm>
- using namespace std;
- #define MAX_N 20
- #define EMPTY_VALUE -1
- int mem[MAX_N];
- int next_index[MAX_N];
- int f(int i, vector<int> &A) {
- if (mem[i] != EMPTY_VALUE) {
- return mem[i];
- }
- int ans = 0;
- for (int j = i + 1; j < A.size(); j++) {
- if (A[j] > A[i]) {
- int subResult = f(j, A);
- if (subResult > ans) {
- ans = subResult;
- next_index[i] = j;
- }
- }
- }
- mem[i] = ans + 1;
- return mem[i];
- }
- vector<int> findLIS(vector<int> A) {
- int ans = 0;
- for (int i = 0; i < A.size(); i++) {
- mem[i] = EMPTY_VALUE;
- next_index[i] = EMPTY_VALUE;
- }
- int start_index = -1;
- for (int i = 0; i < A.size(); i++) {
- int result = f(i, A);
- if (result > ans) {
- ans = result;
- start_index = i;
- }
- }
- vector<int> lis;
- while (start_index != -1) {
- lis.push_back(A[start_index]);
- start_index = next_index[start_index];
- }
- return lis;
- }
- int main() {
- vector<int> input;
- int n, val;
- cout << "Enter the number of elements: ";
- cin >> n;
- cout << "Enter the elements: ";
- for (int i = 0; i < n; i++) {
- cin >> val;
- input.push_back(val);
- }
- vector<int> lis = findLIS(input);
- cout << "Longest Increasing Subsequence: ";
- for (int num : lis) {
- cout << num << " ";
- }
- cout << endl;
- cout << "Length of LIS: " << lis.size() << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement