acm 堆栈stack 使用
来源:互联网 发布:淘宝上好看的男装 编辑:程序博客网 时间:2024/06/01 19:07
Stack堆栈容器是一种实现了先进后出的线性表,它的插入和删除操作只能在栈顶实现。前面的博文中已经说过了,容器适配器包括:queue、stack、priority_queue
这些适配器都是包装了vector、list、deque中某个顺序容器的包装器,将一些基础容器转换成自己的容器。值得注意的是,适配器没有提供迭代器,不能同时插入和删除多个元素。使用stack前需要引入<stack>头文件。
(一)stack堆栈容器对象的创建:
stack<类型> 对象:stack<int> s;
(二)stack常用的操作:
empty() 堆栈为空则返回真;
pop() 移除栈顶元素;
push() 在栈顶增加元素;
size() 返回栈中元素数目;
top() 返回栈顶元素。
[cpp] view plain copy
#include<iostream>
#include<stack>
using namespace std;
int main()
{
stack<int> s;
s.push(1);
s.push(2);
s.push(3);
cout<<"size="<<s.size()<<endl;
while(!s.empty())
{
cout<<s.top()<<" ";
s.pop();
}
cout<<endl;
return 0;
}
(三)stack清空的方法:
[cpp] view plain copy
while(s.empty()==false)
s.pop();
stack应用:
某城市有一个火车站,有n节车厢从A方向驶入车站,按进站的顺序编号为1~n。现让它们按照某种特定的顺序进入B方向的铁轨并驶出车站。为了重组车厢,你可以借助中转站C。在程序中输入车厢数目和出站的特定顺序,如果可以则输出Yes,否者输出No。
样例输入:
5
1 2 3 4 5
5
5 4 1 2 3
6
6 5 4 3 2 1
样例输出:
Yes
No
Yes
[cpp] view plain copy
#include<iostream>
#include<fstream>
#include<deque>
#include<stack>
using namespace std;
int main()
{
int n,s,i;
deque<int> inque;
deque<int> outque;
stack<int> sta;
deque<int>::iterator p;
while(cin>>n)
{
inque.clear();
for(i=0;i<n;i++)
{
cin>>s;
//将出站的顺序写到inque中
inque.push_back(s);
}
//原序队列输出
outque.clear();
for(i=1;i<=n;i++)
{
outque.push_back(i);
}
//清空栈
while(!sta.empty())
{
sta.pop();
}
//flag标志,为1时可以正确出站,为0不可以
int flag=1;
while(!inque.empty())
{
if(sta.empty())
{
sta.push(outque.front());
//outque.front();
}
else
{
if(sta.top()==inque.front())
{
sta.pop();
inque.pop_front();
}
else if(sta.top()<inque.front())
{
sta.push(outque.front());
outque.pop_front();
}
else
{
flag=0;
break;
}
}
}
if(flag==1)
{
cout<<"Yes"<<endl;
}
else
{
cout<<"No"<<endl;
}
}
return 0;
}
括号匹配问题:
[cpp] view plain copy
#include<iostream>
#include<string>
#include<stack>
using namespace std;
int main()
{
int n;
cin>>n;
while(n--)
{
string s;
cin>>s;
stack<char> S;
S.push('#');
for(int i = 0; i<s.size(); ++i)
{
if(s[i]-S.top()==1 || s[i]-S.top()==2)
{
S.pop();
}
else
S.push(s[i]);
}
if(S.size()==1)
cout<<"Yes"<<endl;
else cout<<"No"<<endl;
}
return 0;
}
这些适配器都是包装了vector、list、deque中某个顺序容器的包装器,将一些基础容器转换成自己的容器。值得注意的是,适配器没有提供迭代器,不能同时插入和删除多个元素。使用stack前需要引入<stack>头文件。
(一)stack堆栈容器对象的创建:
stack<类型> 对象:stack<int> s;
(二)stack常用的操作:
empty() 堆栈为空则返回真;
pop() 移除栈顶元素;
push() 在栈顶增加元素;
size() 返回栈中元素数目;
top() 返回栈顶元素。
[cpp] view plain copy
#include<iostream>
#include<stack>
using namespace std;
int main()
{
stack<int> s;
s.push(1);
s.push(2);
s.push(3);
cout<<"size="<<s.size()<<endl;
while(!s.empty())
{
cout<<s.top()<<" ";
s.pop();
}
cout<<endl;
return 0;
}
(三)stack清空的方法:
[cpp] view plain copy
while(s.empty()==false)
s.pop();
stack应用:
某城市有一个火车站,有n节车厢从A方向驶入车站,按进站的顺序编号为1~n。现让它们按照某种特定的顺序进入B方向的铁轨并驶出车站。为了重组车厢,你可以借助中转站C。在程序中输入车厢数目和出站的特定顺序,如果可以则输出Yes,否者输出No。
样例输入:
5
1 2 3 4 5
5
5 4 1 2 3
6
6 5 4 3 2 1
样例输出:
Yes
No
Yes
[cpp] view plain copy
#include<iostream>
#include<fstream>
#include<deque>
#include<stack>
using namespace std;
int main()
{
int n,s,i;
deque<int> inque;
deque<int> outque;
stack<int> sta;
deque<int>::iterator p;
while(cin>>n)
{
inque.clear();
for(i=0;i<n;i++)
{
cin>>s;
//将出站的顺序写到inque中
inque.push_back(s);
}
//原序队列输出
outque.clear();
for(i=1;i<=n;i++)
{
outque.push_back(i);
}
//清空栈
while(!sta.empty())
{
sta.pop();
}
//flag标志,为1时可以正确出站,为0不可以
int flag=1;
while(!inque.empty())
{
if(sta.empty())
{
sta.push(outque.front());
//outque.front();
}
else
{
if(sta.top()==inque.front())
{
sta.pop();
inque.pop_front();
}
else if(sta.top()<inque.front())
{
sta.push(outque.front());
outque.pop_front();
}
else
{
flag=0;
break;
}
}
}
if(flag==1)
{
cout<<"Yes"<<endl;
}
else
{
cout<<"No"<<endl;
}
}
return 0;
}
括号匹配问题:
[cpp] view plain copy
#include<iostream>
#include<string>
#include<stack>
using namespace std;
int main()
{
int n;
cin>>n;
while(n--)
{
string s;
cin>>s;
stack<char> S;
S.push('#');
for(int i = 0; i<s.size(); ++i)
{
if(s[i]-S.top()==1 || s[i]-S.top()==2)
{
S.pop();
}
else
S.push(s[i]);
}
if(S.size()==1)
cout<<"Yes"<<endl;
else cout<<"No"<<endl;
}
return 0;
}
阅读全文
0 0
- acm 堆栈stack 使用
- ACM学习历程18——stack堆栈容器
- 堆栈(stack)
- 堆栈stack
- 使用Stack堆栈集合大数据运算
- 使用堆栈(Stack)模拟实现队列(FIFO)
- acm中Stack和Queue使用demo
- 使用PerlRegex出现“Stack overflow”堆栈溢出的问题
- NDK 异常堆栈 查看工具 ndk-stack 的使用
- java堆栈类使用实例(java中stack的使用方法
- java堆栈类使用实例(java中stack的使用方法)
- java堆栈类使用实例(java中stack的使用方法)
- java堆栈类使用实例(java中stack的使用方法)
- 堆栈数据结构 stack.h
- 堆栈(Stack)示例
- /STACK(堆栈分配)
- 堆栈(Stack)
- stack heap 堆栈
- Mysql 锁问题分析 InnoDB 锁
- Markdown编辑器初步使用
- Javascript 普通函数和构造函数的区别
- 51单片机按键控制输出pwm的占空比
- 利用Java读取并写入Word文件
- acm 堆栈stack 使用
- QZOI滚粗记
- Oracle如何修改字符集及如何查看dmp文件的字符集信息
- 图文详解YUV420数据格式
- android gps机制分析--之七
- [leetcode]: 191. Number of 1 Bits
- tcp/ip 编程之进阶api
- 《An Introduction to Ray Tracing》—— 2.4 Ray/Box Intersection
- sqlserver 注释查看