-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathcode_1.cpp
84 lines (72 loc) · 1.89 KB
/
code_1.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
//
// code_1.cpp
// Algorithm
//
// Created by Mohd Shoaib Rayeen on 23/11/18.
// Copyright © 2018 Shoaib Rayeen. All rights reserved.
//
#include <iostream>
#include <vector>
using namespace std;
void LCIS(vector<int>arr1 , vector<int>arr2) {
int n = int(arr1.size());
int m = int(arr2.size());
int table[m] , parent[m];
for (int j = 0; j < m; j++) {
table[j] = 0;
}
for (int i = 0; i < n; i++) {
int current = 0 , last = -1;
for (int j = 0; j < m; j++) {
if (arr1[i] == arr2[j]) {
if (current + 1 > table[j]) {
table[j] = current + 1;
parent[j] = last;
}
}
if (arr1[i] > arr2[j]) {
if (table[j] > current) {
current = table[j];
last = j;
}
}
}
}
int result = 0 , index = -1;
for (int i = 0; i < m; i++) {
if (table[i] > result) {
result = table[i];
index = i;
}
}
int valueLCIS[result];
for (int i = 0; index != -1; i++) {
valueLCIS[i] = arr2[index];
index = parent[index];
}
cout << "\nLength of LCIS\t:\t " << result;
cout << "\nLCIS\t:\t";
for (int i = result - 1; i >= 0; i-- ) {
cout << valueLCIS[i] << " ";
}
cout << endl;
}
int main() {
int n;
cout << "\nEnter Size of First Array\t:\t";
cin >> n;
vector <int> array1(n);
cout << "\nEnter Elements of First Array\n";
for ( int i = 0 ;i < n;i++) {
cin >> array1[i];
}
cout << "\nEnter Size of Second Array\t:\t";
cin >> n;
vector <int> array2(n);
cout << "\nEnter Elements of First Array\n";
for ( int i = 0 ;i < n;i++) {
cin >> array2[i];
}
LCIS(array1 , array2);
return 0;
}