/*
一个序列判断是不是另一个序列栈顺序的合法输出
*/
#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]
最新推荐文章于 2022-10-20 14:18:58 发布
本文介绍了一个简单的C++程序,用于判断一个整数序列是否为另一个序列通过栈操作得到的合法输出。通过使用标准模板库(stack)实现栈的功能,并采用迭代方式检查两个序列之间的关系。
1万+

被折叠的 条评论
为什么被折叠?



