锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
2 #include <stack>
3
4 using namespace std;
5 /*!
6 \brief This algorithm demostrates how to judge whether or not a given seqence is
7 possibly a popup sequence providing the pushing sequence.
8 */
9
10 bool IsPopSequence(int* ppush, int pushm, int* ppop, int popm)
11 {
12 int i = 0, j = 0;
13 stack<int> help_stack;
14 while(j < popm)
15 {
16 if (!help_stack.empty() && help_stack.top() == ppop[j])
17 {
18 help_stack.pop();
19 j++;
20 }
21 else if (i < pushm)
22 {
23 help_stack.push(ppush[i++]);
24 }
25 else
26 break;
27 }
28
29 return j >= popm;
30 }
31
32 int main(int argc, char *argv, char *env[])
33 {
34 //int pushseq[] = {1, 2, 3, 4, 5};
35 //int pushseqlen = sizeof(pushseq)/sizeof(pushseq[0]);
36 ////int popseq[] = {4, 5, 3, 2, 1};
37 //int popseq[] = {4, 3, 5, 1, 2};
38 //int popseqlen = sizeof(popseq)/sizeof(popseq[0]);
39
40 int pushseq[] = {1, 2, 3, 4, 5, 6};
41 int popseq[] = {4, 3, 5, 2, 1};
42 int pushseqlen = sizeof(pushseq)/sizeof(pushseq[0]);
43 int popseqlen = sizeof(popseq)/sizeof(popseq[0]);
44
45 cout<<boolalpha<<IsPopSequence(pushseq, pushseqlen, popseq, popseqlen)<<"\n";
46
47 return 0;
48 }