Description:
Enter Two integer sequences. the first sequence indicates the order in which the stack is pushed. Determine whether the second sequence is in the pop-up order of the stack. Assume that all numbers pushed into the stack are not equal. For example, the sequence 1, 2, 3, 4, and 5 is the order in which a stack is pushed. The sequence 4, 5, 3, 2, and 1 is a pop-up sequence corresponding to the stack sequence, but the sequence 4, 3, 5, 2. It cannot be the pop-up sequence of the Pressure stack sequence.
AC code:
bool isStackSequence(int *input,int *output,int len) { /*if(input==NULL || output==NULL) { throw new exception("the input is error"); }*/ stack<int> st; while(len--) { st.push(*input++); while(!st.empty() && st.top()==*output) { st.pop(); output++; } } if(st.empty()) { return true; }else { return false; } } int main(int argc, const char * argv[]) { int n,i; //fstream cin("input.txt"); while(cin>>n) { int *input=new int[n]; int *output=new int[n]; for(i=0;i<n;i++) { cin>>input[i]; } for(i=0;i<n;i++) { cin>>output[i]; } if(isStackSequence(input,output,n)) { cout<<"Yes"<<endl; }else { cout<<"No"<<endl; } delete []input; delete []output; } //getchar(); return 0; }