最长递增子序列算法尚有缺陷,不能输出所有最长递增子序列
最长递增子序列:
#include<iostream>
using namespace std;
void PrintMaxSubSeq(int flag, int i, int *a, int *prev)
{
if (prev[i] == i) { cout << a[i] << " "; return; }
PrintMaxSubSeq(1, prev[i], a, prev);
cout << a[i] << " ";
if (0 == flag)cout << endl; //是否是第一次进栈
}
int main()
{
int a[10] = { 7,8,9,5,1,2,3,10,11,12 }; //随便写个数组,此处不做用户输入了
int count[10]; //标记以当前数字为末尾的最长子序列长度
int prev[10]; //标记以当前数字为末尾的最长子序列倒数第二个数字序号
int maxx = 0;
for (int i = 0; i < 10; i++)
{
count[i] = 1;
prev[i] = i;
for (int j = 0; j < i; j++)
{
if (a[i] > a[j] && count[i] < count[j] + 1)
{
count[i] = count[j] + 1;
prev[i] = j;
maxx = maxx < count[i] ? count[i] : maxx;
}
}
}
for (int i = 0; i < 10; i++)
{
if (maxx == count[i])
{