/************ 1 2 3 4 如栈 出栈 3 2 4 1 返回0 错误 3 1 2 4 返回-1 **********/ #include<iostream> #include<stack> #include<string.h> using namespace std; bool judge(const char *push_seq, const char *pop_seq) { if (NULL == push_seq || NULL == pop_seq) { return false; } if (strlen(push_seq) != strlen(pop_seq)) { return false; } stack<char>sc; while (*push_seq) { if (0 == sc.size() || sc.top() != *pop_seq) { sc.push(*push_seq++); // } else { sc.pop(); ++pop_seq; // } } while (sc.size()) { if (sc.top() != *pop_seq++) { return false; } sc.pop(); } return true; } int mycheck1(int a[],int n) { stack<int>s; int i,j,k=1; // 3 2 4 1 for(i=0;i<n;i++) { if(a[i]>k) { for(j=k;j<a[i];j++) s.push(j); k=j+1;cout<<k<<endl; continue; } if(a[i]==s.top()){s.pop();continue;} if(a[i]<s.top()) return -1; } cout<<endl; return 0; } int mycheck(int a[],int n) { stack<int>s; s.push(0); int i,j,k=1; //BUG段 经过分析 在于s.top段 因为没有元素时这样做有越界的含义 for(i=0;i<n;i++) { if(a[i]>k) { for(j=k;j<a[i];j++) s.push(j); k=j+1;cout<<k<<endl; continue; } if(a[i]==s.top()){s.pop();continue;} if(a[i]<s.top()) return -1; } cout<<endl; return 0; } int main() { //cout<<judge("1234","3241")<<endl; //int a[]={1,2,3,4}; int a[]={3,2,4,1}; // int a[]={4,3,2,1};ok cout<<mycheck(a,4); return 0; }
时间: 2024-10-06 01:27:49