/*
一个序列判断是不是另一个序列栈顺序的合法输出
*/
#include <iostream>
#include <stack>
bool isTheStack(int a[], int b[], int n)
{
std::stack<int> ts;
int i = 0;
int j = 0;
while(i < n && j < n)
{
if(ts.empty() || ts.top() != b[j])
ts.push(a[++i]);
else
{
ts.pop();
j++;
}
}
return ts.empty();
}
int main()
{
int a[] = {1, 2, 3, 4, 5};
int b[] = {5, 4, 2, 3, 1};
std::cout << isTheStack(a, b, 5) << std::endl;
return 0;
}
题目05302012 [1]
最新推荐文章于 2021-05-18 01:59:26 发布