最長子序列

#include <vector>
#include <iostream>
using namespace std;

int main()
{
	constexpr int num = 8;
	int a[num] = {1,7,8,4,3,2,5,6};
	std::vector<std::vector<int>> result[num];
	result[0].push_back(std::vector<int>());
	result[0][0].push_back(a[0]);
	for (int i = 1; i < num; ++i) {
		result[i].push_back(std::vector<int>());
		result[i][0].push_back(a[i]);
		//與之前的所有的遞增子序列比較
		for (int j = 0; j < i; ++j) {
			for (int k = 0; k < result[j].size(); ++k) {
				//如果大於之前的那些子序列的最後的值,則複製並+1, 如果沒有大於直接複製
				result[i].push_back(std::vector<int>());
				for (auto k : result[j][k]) {
					result[i][result[i].size() - 1].push_back(k);
				}
				if (a[i] > result[j][k][result[j][k].size() - 1]) {
					result[i][result[i].size() - 1].push_back(a[i]);
				}
			}
		}
	}
	int i = 0;
	for (auto k : result) {
		i++;
		cout <<"F(" <<i <<"): " <<endl;
		for (auto k1 : k) {
			for (auto k2 : k1) {
				cout << k2 << " ";
			}
			cout << endl;
		}
		cout << endl;
	}
    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章