Array implementations:
#include <stdio.h>#define N -intMain () {intI, N, Target[n]; while(scanf("%d", &n) = =1) {int Stack[N], top =-1; for(i =0; I < n; i++)scanf("%d", &target[i]);intA =1, B =0;intOK =1; while(B < N) {if(A = = Target[b]) {a++; b++; }Else if(Top >=0&&Stack[Top] = = Target[b]) {top--; b++; }Else if(A <= N) {Stack[++top] = a++; }Else{OK =0; Break; } }printf("%s\n"Ok?"Yes":"No"); }return 0;}
STL implementations:
#include <cstdio>#include <stack>#define N -using namespace STD;intMain () {intI, N, Target[n]; while(scanf("%d", &n) = =1) { Stack<int>S for(i =0; I < n; i++)scanf("%d", &target[i]);intA =1, B =0, OK =1; while(B < N) {if(A = = Target[b]) {a++; b++; }Else if(!s.empty () && s.top () = = Target[b]) {S.pop (); b++; }Else if(A <= N) {S.push (a++);}Else{OK =0; Break; } }printf("%s\n"Ok?"Yes":"No"); }return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Stack (LIFO)