This repository has been archived by the owner on Dec 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
LIS.cpp
124 lines (114 loc) · 2.39 KB
/
LIS.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*************************************************************************
> File Name: LIS.cpp
> Author: GZ
> Mail: 194463810@qq.com
> Created Time: 2018年04月15日 星期日 21时14分27秒
************************************************************************/
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
const int INFINITY = 9999;
void LIS(vector<int> a);
void LIS(vector<int> a, vector<int>& result);
int main(void)
{
vector<int> a(5);
cout << "请输入5个数据:";
for (int i = 0; i < 5; i++)
cin >> a[i];
LIS(a);
return 0;
}
void LIS(vector<int> a)
{
int n = a.size();
vector<int> L(n);
L[0] = 1;
vector<int> prev(n); //前驱结点
for (int i = 0; i < n; i++)
prev[i] = INFINITY;
//填L[]数组
for (int i = 1; i < n; i++)
{
int max = 1;
int u = 0;
for (int k = 0; k < i; k++)
if (a[k] < a[i])
{
u = L[k] + 1;
if (max < u)
{
max = u;
prev[i] = k;
}
}
L[i] = max;
}
//找最长子序列的长度
int max = 0;
int index = 0;
for (int i = 0; i < n; i++)
if (max < L[i])
{
max = L[i];
index = i;
}
cout << "最长递归子序列的长度为:" << max << endl;
/*
cout << "输出L: ";
for (int i = 0; i < n; i++)
cout << L[i] << " ";
cout << endl;
cout << "输出prev: ";
for (int i = 0; i < n; i++)
cout << prev[i] << " ";
cout << endl;
*/
//输出最长递归子序列
vector<int> substr(max);
int k = 0;
while (prev[index] != INFINITY)
{
substr[k++] = a[index];
index = prev[index];
}
substr[k] = a[index];
cout << "最长递增子序列为:";
for (k = max - 1; k > 0; k--)
cout << substr[k] << " ";
cout << substr[k] << endl;
}
void LIS(vector<int> a, vector<int>& result)
{
vector<int> b(a);
sort(b.begin() + 1, b.end());
//LCS问题,得到表L[n][m]
int n = a.size();
int m = b.size();
vector<vector<int> > L(n);
for (int i = 0; i < n; i++)
L[i].resize(m);
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
{
if (i == 0 || j == 0) L[i][j] = 0;
else if (a[i] == b[j])
L[i][j] = L[i - 1][j - 1] + 1;
else L[i][j] = (L[i - 1][j] > L[i][j - 1]) ? L[i - 1][j] : L[i][j - 1];
}
//得到LCS
int i = n - 1;
int j = m - 1;;
while (i > 0 && j > 0)
{
if (a[i] == b[j])
{
result.push_back(a[i]);
i--;
j--;
}
else if (L[i - 1][j] > L[i][j - 1]) j--;
else i--;
}
}