题解 | #火车进站#
火车进站
https://www.nowcoder.com/practice/97ba57c35e9f4749826dc3befaeae109
#include <iostream>
#include <algorithm>
#include <stack>
#include <vector>
using namespace std;
vector<int> T,T1;
bool func() {
stack<int> ST;
int i = 0;
int j;
int len=T.size();
for(j=0;j<len;j++){
ST.push(T[j]);
while (!ST.empty()&&ST.top()==T1[i]) {
ST.pop();
i++;
}
}
if(ST.empty()) return true;
return false;
}
int main() {
int i, n, j;
cin >> n;
for(i=1;i<=n;i++){
cin>>j;
T.push_back(j);
T1.push_back(j);
}
sort(T1.begin(), T1.end());
do {
if (func()) {
for (i = 0; i < n; i++) {
cout << T1[i] << ' ';
}
cout << endl;
}
} while (next_permutation(T1.begin(), T1.end()));
return 0;
}
// 64 位输出请用 printf("%lld")
vector初始化和next_permutation
