题解 | #火车进站#

火车进站

https://www.nowcoder.com/practice/97ba57c35e9f4749826dc3befaeae109

// 1. 使用全排列 next_permutation 进行操作
// 2. 使用全排列 next_permutation 前需要先进行 sort 排序操作
// 3. 使用 stack 栈 这个数据结构。 

#include <algorithm>
#include<iostream>
#include <stack>
#include <vector>

using namespace std;

int n;
vector<int> v;
vector<int> t;
bool check(vector<int> & t){
    bool bisOk = true;

    stack<int> st;
    int j = 0;
    for(int i = 0; i < n; ++i){
        st.push(v[i]);
        while(!st.empty() && st.top() == t[j]){
            st.pop();
            j++;
        }
    }

    return (j == n);
}

int main(){
    while(cin >> n){
        int tmp;
        for(int i = 0; i < n; ++i){
            cin >> tmp;
            v.push_back(tmp);
            t.push_back(tmp);
        }
        sort(t.begin(), t.end());
        do{
            if(check(t)){
                for(auto it : t){
                    cout << it << " ";
                }
                cout << endl;
            }
        }while(next_permutation(t.begin(), t.end()));
        
    }
    return 0;
}

全部评论

相关推荐

10-22 12:03
山东大学 Java
程序员小白条:26届一般都得有实习,项目可以随便写的,如果不是开源社区的项目,随便包装,技术栈也是一样,所以本质应该找学历厂,多投投央国企和银行,技术要求稍微低一点的,或者国企控股那种,纯互联网一般都得要干活
应届生简历当中,HR最关...
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务