-
Notifications
You must be signed in to change notification settings - Fork 0
/
is_poporder_lqC.cpp
62 lines (60 loc) · 1.24 KB
/
is_poporder_lqC.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <iostream>
#include <stack>
using namespace std;
const int maxn = 1000;
int A[maxn];
int B[maxn];
bool IsPopOrder(const int *push, const int *pop, int length)
{
bool bPossible = false;
if (push != NULL && pop != NULL && length > 0)
{
std::stack<int> stackData;
int i = 0;
int j = 0;
while (j < length)
{
while (stackData.empty() || stackData.top() != pop[j])
{
if (i > length - 1)
break;
stackData.push(push[i]);
++i;
}
if (stackData.top() != pop[j])
break;
stackData.pop();
++j;
}
if (stackData.empty() && j == length)
bPossible = true;
}
return bPossible;
}
int main()
{
int count = 0;
char c;
while ((c = getchar()) != '\n')
{
if (c != ' ')
{
A[count] = c - '0';
count++;
}
}
count = 0;
while ((c = getchar()) != '\n')
{
if (c != ' ')
{
B[count] = c - '0';
count++;
}
}
if (IsPopOrder(A, B, count + 1))
cout << "True";
else
cout << "False";
return 0;
}