Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- #include <string>
- #include <sstream>
- #define SIZE 100
- #define TAB 3
- using namespace std;
- void LIS (int A[], int C[], int N) {
- C[0] = 1;
- int max;
- for (int i = 1; i < N; i++) {
- max = 0;
- for (int j = i - 1; j >= 0; j--)
- if (C[j] > max && A[j] < A[i])
- max = C[j];
- C[i] = 1 + max;
- }
- }
- int findLIS (int A[], int B[], int C[], int N) {
- int max = 0;
- int pos = 0;
- for (int i = N - 1; i >= 0; i--)
- if (C[i] > max) {
- max = C[i];
- pos = i;
- }
- int n = max;
- for (int i = pos; i >= 0; i--)
- if (C[i] == n)
- B[--n] = A[i];
- return max;
- }
- void printSequence (int B[], int n) {
- for (int i = 0; i < n; i++)
- cout << B[i] << " ";
- cout << endl;
- }
- void printArrays (int A[], int C[], int N) {
- cout << "A: ";
- for (int i = 0; i < N; i++)
- cout << setw(TAB) << A[i];
- cout << endl;
- cout << "C: ";
- for (int i = 0; i < N; i++)
- cout << setw(TAB) << C[i];
- cout << endl;
- }
- int main () {
- string line;
- cout << "Enter the Array: ";
- getline(cin, line);
- istringstream iss(line);
- int N, n;
- int A[SIZE];
- int B[SIZE];
- int C[SIZE];
- for (N = 0; iss >> line; N++)
- stringstream(line) >> A[N];
- LIS (A, C, N);
- n = findLIS(A, B, C, N);
- cout << "Length of Longest Increasing Subsequence: " << n << endl;
- cout << "Longest Increasing Subsequence: ";
- printSequence(B, n);
- printArrays(A, C, N);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement