题解 | 火车进站

火车进站

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

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

using namespace std;
int n;
int id[11];
vector<int> arr;

bool cmp() {
    stack<int> s;
    int j = 0;
    for (int i = 0; i < n; ++i) {
        s.push(id[i]);
        while (!s.empty() && s.top() == arr[j]) {
            s.pop();
            j++;
        }
    }
    return s.empty() && j == n;
}
int main() {
    cin >> n;
    for (int i = 0; i < n; ++i) {
        cin >> id[i];
    }
    arr.resize(n);
    iota(arr.begin(),arr.end(),1);
    do {
        if (cmp()) {
            cout << arr[0];
            for (int i = 1; i < n; ++i)
                cout << ' ' << arr[i];
            cout << endl;
        }
    } while (next_permutation(arr.begin(),arr.end()));
    return 0;
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

06-25 21:00
门头沟学院 Java
多拆解背记一下当前的高频场景面试题,结合自己的项目经历去作答,面试通过率原来真的不会低!
牛客965593684号:小公司不就是这样的吗,面试要么是点击就送,要么就是往死里拷打,没有一个统一的标准。这个不能代表所有公司
点赞 评论 收藏
分享
每晚夜里独自颤抖:你cet6就cet6,cet4就cet4,你写个cet证书等是什么意思。专业技能快赶上项目行数,你做的这2个项目哪里能提现你有这么多技能呢
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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