题解 | #火车进站#
火车进站
https://www.nowcoder.com/practice/97ba57c35e9f4749826dc3befaeae109
#include <algorithm> #include <iostream> #include <iterator> #include <stack> #include <unistd.h> #include <vector> using namespace std; vector<int> en_ivec; bool check(vector<int> &ivec) { stack<int> st; for(int i = 0, idx_ivec = 0; i < ivec.size(); ++ i) { st.push(en_ivec.at(i)); while(!st.empty() && st.top() == ivec.at(idx_ivec)) { st.pop(); ++ idx_ivec; } } return st.empty(); } int main() { vector<int> ivec; istream_iterator<int> intCin(cin); istream_iterator<int> intCinEOF; ++ intCin; while(intCin != intCinEOF) { ivec.push_back(*intCin ++); } en_ivec = ivec; ostream_iterator<int> intCout(cout, " "); sort(ivec.begin(), ivec.end()); do{ if(check(ivec)) { copy(ivec.cbegin(), ivec.cend(), intCout); cout << endl; } }while(next_permutation(ivec.begin(), ivec.end())); return 0; } // 64 位输出请用 printf("%lld")