-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlCS_13.cpp
63 lines (63 loc) · 1.25 KB
/
lCS_13.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
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n, i, j, x, y;
vector<int> a, b, c;
cout << "Enter no. of integers:" << endl;
cin >> n;
cout << "Enter Integers:" << endl;
for (i = 0; i < n; i++)
{
cin >> j;
a.push_back(j);
b.push_back(j);
}
sort(b.begin(), b.end());
int m[n + 1][n + 1];
for (i = 0; i <= n; i++)
{
m[i][0] = 0;
m[0][i] = 0;
}
for (i = 1; i <= n; i++)
{
for (j = 1; j <= n; j++)
{
if (a[i - 1] != b[j - 1])
{
m[i][j] = max(m[i][j - 1], m[i - 1][j]);
}
else
{
m[i][j] = m[i - 1][j - 1] + 1;
}
}
}
cout << m[n][n] << endl;
x = n;
y = n;
while (m[x][y] > 0)
{
if (m[x][y] == m[x - 1][y])
{
x--;
}
else if (m[x][y] == m[x][y - 1])
{
y--;
}
else
{
x--;
y--;
c.push_back(b[y]);
}
}
reverse(c.begin(), c.end());
for (i = 0; i < c.size(); i++)
{
cout << c[i] << " ";
}
return 0;
}