栈的push pop序列

题目:输入两个整数序列。其中一个序列表示栈的push顺序,
判断另一个序列有没有可能是对应的pop顺序。
为了简单起见,我们假设push序列的任意两个整数都是不相等的。 比如输入的push序列是1、2、3、4、5,那么4、5、3、2、1就有可能是一个pop系列。
因为可以有如下的push和pop序列:
push 1,push 2,push 3,push 4,pop,push 5,pop,pop,pop,pop,
这样得到的pop序列就是4、5、3、2、1。
但序列4、3、5、1、2就不可能是push序列1、2、3、4、5的pop序列。

 

分析:这到题除了考查对栈这一基本数据结构的理解,还能考查我们的分析能力。

这道题的一个很直观的想法就是建立一个辅助栈,每次push的时候就把一个整数push进入这个辅助栈,同样需要pop的时候就把该栈的栈顶整数pop出来。

我们以前面的序列45321为例。第一个希望被pop出来的数字是4,因此4需要先push到栈里面。由于push的顺序已经由push序列确定了,也就是在把4 push进栈之前,数字123都需要push到栈里面。此时栈里的包含4个数字,分别是1234,其中4位于栈顶。把4 pop出栈后,剩下三个数字123。接下来希望被pop的是5,由于仍然不是栈顶数字,我们接着在push序列中4以后的数字中寻找。找到数字5后再一次push进栈,这个时候5就是位于栈顶,可以被pop出来。接下来希望被pop的三个数字是321。每次操作前都位于栈顶,直接pop即可。

再来看序列43512pop数字4的情况和前面一样。把4 pop出来之后,3位于栈顶,直接pop。接下来希望pop的数字是5,由于5不是栈顶数字,我们到push序列中没有被push进栈的数字中去搜索该数字,幸运的时候能够找到5,于是把5 push进入栈。此时pop 5之后,栈内包含两个数字12,其中2位于栈顶。这个时候希望pop的数字是1,由于不是栈顶数字,我们需要到push序列中还没有被push进栈的数字中去搜索该数字。但此时push序列中所有数字都已被push进入栈,因此该序列不可能是一个pop序列。

也就是说,如果我们希望pop的数字正好是栈顶数字,直接pop出栈即可;如果希望pop的数字目前不在栈顶,我们就到push序列中还没有被push到栈里的数字中去搜索这个数字,并把在它之前的所有数字都push进栈。如果所有的数字都被push进栈仍然没有找到这个数字,表明该序列不可能是一个pop序列。

基于前面的分析,我们可以写出如下的参考代码

 

 

具体代码如下:

[html]  view plain copy
  1. // IsPossiblePopOrder.cpp : 定义控制台应用程序的入口点。  
  2. //  
  3.   
  4. #include "stdafx.h"  
  5. #include <iostream>  
  6. #include <stack>  
  7. using namespace std;  
  8.   
  9. bool IsPossiblePopOrder(const int* pPush, const int* pPop, int nLength)  
  10. {  
  11.     bool bPossible = false;  
  12.   
  13.     if(pPush && pPop && nLength > 0)  
  14.     {  
  15.         const int *pNextPush = pPush;  
  16.         const int *pNextPop = pPop;  
  17.   
  18.         // ancillary stack  
  19.         std::stack<int> stackData;  
  20.   
  21.         // check every integers in pPop  
  22.         while(pNextPop - pPop < nLength)  
  23.         {  
  24.             // while the top of the ancillary stack is not the integer   
  25.             // to be poped, try to push some integers into the stack  
  26.             while(stackData.empty() || stackData.top() != *pNextPop)  
  27.             {  
  28.                 // pNextPush == NULL means all integers have been   
  29.                 // pushed into the stack, can't push any longer  
  30.                 if(!pNextPush)  
  31.                     break;  
  32.   
  33.                 stackData.push(*pNextPush);  
  34.   
  35.                 // if there are integers left in pPush, move   
  36.                 // pNextPush forward, otherwise set it to be NULL  
  37.                 if(pNextPush - pPush < nLength - 1)  
  38.                     pNextPush ++;  
  39.                 else  
  40.                     pNextPush = NULL;  
  41.             }  
  42.   
  43.             // After pushing, the top of stack is still not same as   
  44.             // pPextPop, pPextPop is not in a pop sequence  
  45.             // corresponding to pPush  
  46.             if(stackData.top() != *pNextPop)  
  47.                 break;  
  48.   
  49.             // Check the next integer in pPop  
  50.             stackData.pop();  
  51.             pNextPop ++;  
  52.         }  
  53.   
  54.         // if all integers in pPop have been check successfully,   
  55.         // pPop is a pop sequence corresponding to pPush   
  56.         if(stackData.empty() && pNextPop - pPop == nLength)  
  57.             bPossible = true;  
  58.     }  
  59.   
  60.     return bPossible;  
  61. }  
  62.   
  63.   
  64.   
  65. int _tmain(int argc, _TCHAR* argv[])  
  66. {  
  67.     int a[]={1,2,3,4,5};  
  68.     int b[]={4,5,3,2,1};  
  69.     int c[]={4,3,5,1,2};  
  70.     cout<<IsPossiblePopOrder(a,c,sizeof(a)/sizeof(int));  
  71.     system("pause");  
  72.   
  73.     return 0;  
  74. }  

其他简单些代码如下

 1: #include<iostream> 
  2: #include<stack> 
  3: using namespace std; 
  4:  
  5: bool IsPushSeriesCorrection(int push[],int pop[],int n) 
  6: { 
  7:     int p1=0,p2=0; 
  8:     stack<int> mystack; 
  9:     while(p2<n) 
 10:     { 
 11:         while(mystack.empty() || mystack.top()!=pop[p2]) 
 12:         { 
 13:             if(p1<n) 
 14:                 mystack.push(push[p1++]); 
 15:             else 
 16:                 return false; 
 17:         } 
 18:          
 19:         while(!mystack.empty() && mystack.top()==pop[p2]) 
 20:         { 
 21:             mystack.pop(); 
 22:             p2++; 
 23:         } 
 24:     } 
 25:     return true; 
 26: } 
 27:  
 28:  
 29: int main() 
 30: { 
 31:     int push[]={1,2,3,4,5}; 
 32:     //int pop[]={4,3,5,1,2}; 
 33:     int pop[]={4,3,5,2,1}; 
 34:     int n=sizeof(push)/sizeof(int); 
 35:     if(IsPushSeriesCorrection(push,pop,n)) 
 36:     { 
 37:         printf("该pop序列可能是所给出Push序列的一种pop顺序。\n"); 
 38:     } 
 39:     else 
 40:     { 
 41:         printf("该pop序列不可能是所给出Push序列的一种pop顺序。\n"); 
 42:     } 
 43:  
 44:     getchar(); 
 45:     return 0; 
 46: }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值