1. 程式人生 > >完美世界筆試題-遞增子序列B-最長遞增子序列打印

完美世界筆試題-遞增子序列B-最長遞增子序列打印

spa pan ios ron cnblogs 子序列 bsp sid logs

#include<iostream>
#include<memory.h>
#include<stack>
using namespace std;

const int maxn = 3005;
int A[maxn],d[maxn],fron[maxn];

int f(int i)
{
    for(int j = 0; j < i; j ++)
    {
        if(A[j] < A[i])
        {
            if(d[j] + 1 > d[i])
            {
                d[i] 
= d[j] + 1; fron[i] = j; } } } return d[i]; } int main() { int t; cin >> t; int n; while(t --) { cin >> n; memset(A, 0, sizeof(A)); for(int i = 0; i < n; i ++) { cin >> A[i]; d[i]
= 1; fron[i] = i; } int ans = -1, ansid = 0; for(int i = 0; i < n; i ++) { if(f(i) > ans) { ans = f(i); ansid = i; } } stack<int> st; while(fron[ansid] != ansid) { st.push(A[ansid]); ansid
= fron[ansid]; } st.push(A[ansid]); bool flag = false; while(!st.empty()) { if(flag)cout << ; flag = true; cout << st.top(); st.pop(); } cout << endl; } }

已過。

完美世界筆試題-遞增子序列B-最長遞增子序列打印