stack permutation
#include <iostream> #include <stack> #include <queue> using namespace std; bool checkSP(int in[], int out[], int n) { queue<int> input; for(int i=0;i<n;i++) input.push(in[i]); queue<int> output; for(int i=0;i<n;i++) output.push(out[i]); stack<int> temp; while(!input.empty()) { int elem = input.front(); input.pop(); if(elem == output.front()) { output.pop(); while(!temp.empty()) { if(temp.top() == output.front()) { temp.pop(); output.pop(); } else break; } } else temp.push(elem); } return (input.empty()&&temp.empty()); } int main(){ int input[] = {1,2,3};//<1,2,3] int output[] = {3,1,2};//<3,1,2] int n = 3; if(checkSP(input, output, n)) cout << "YES"; else cout << "NO"; return 0; }