【C++】c++中栈 队列 的应用
C++中提供了STL模板statck 在使用的时候更为方便
除了一般的队列外 还有STL更有双向队列可以使用 #include<deque> 声明:deque <type > name
应用举例1:铁轨问题
Description

The local tradition is that every train arriving from the direction A continues in the direction B with coaches reorganized in some way. Assume that the train arriving from the direction A has N <= 1000 coaches numbered in increasing order 1, 2, ..., N. The chief for train reorganizations must know whether it is possible to marshal coaches continuing in the direction B so that their order will be a1, a2, ..., aN. Help him and write a program that decides whether it is possible to get the required order of coaches. You can assume that single coaches can be disconnected from the train before they enter the station and that they can move themselves until they are on the track in the direction B. You can also suppose that at any time there can be located as many coaches as necessary in the station. But once a coach has entered the station it cannot return to the track in the direction A and also once it has left the station in the direction B it cannot return back to the station.
Input
The last block consists of just one line containing 0.
Output
Sample Input
5
1 2 3 4 5
5 4 1 2 3
0
6
6 5 4 3 2 1
0
0
Sample Output
Yes
No
Yes
/*************************************************************************
> File Name: 6_2.cpp
> Author:KID_XiaoYuan
> Mail:kuailexy@126.com
> Created Time: 2017年06月05日 星期一 18时41分08秒
> 算法描述:Rails :判断输入的数字能否通过栈的方式后输出 如果可以 输出YES否则输出NO
> 样例输入: 5
1 2 3 4 5
3 4 5 2 1
> 样例输出:YES
> 参考更高效代码:p141
************************************************************************/
#include<stack>
#include<iostream>
#define MAX 100
using namespace std;
int input[MAX];
int input2[MAX]; int main()
{
stack<int> s;
int n,j = ,k = ;
scanf("%d",&n);
for(int i = ; i < n; i++)
{
scanf("%d",&input[i]);
} for(int i =; i < n; i++)
{
scanf("%d",&input2[i]);
}
while(j < n)
{
s.push(input[j]);
while(k < n && (s.top() == input2[k]))
{
s.pop();
k++;
}
j++;
}
printf("%s\n",(s.empty()?"YES":"NO")); return ;
}
改进版本:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<stack>
#include<algorithm>
using namespace std;
int main()
{
int n;
int target[];
while(~scanf("%d",&n))///输入也是个大问题啊。。
{
if(n == ) return ;
while(~scanf("%d",&target[]))
{
if(target[] == )///首先判断输入的第一个数
{
puts("");///不要忘记换行.
break;
}
for(int i = ; i <= n; i++)
scanf("%d",&target[i]);
int a,b;
a = b = ;
stack<int> s;
bool mark = true;
while(b <= n)///这就是判断是否符合出栈规则的核心;
{
if(a == target[b])///判断重位的元素;
{
a++;
b++;
}
else if(!s.empty() && s.top() == target[b])///判断先进后出的规则,可以想象成倒叙。
{
s.pop();
b++;
}
else if(a <= n)///之所以能这样,关键是因为入栈的顺序是连续的1~n数字;
{
s.push(a);
a++;
}
else
{
mark = false;
break;
}
}
if(mark == true) printf("Yes\n");
else printf("No\n");
} } }
应用实例2:
Matrix multiplication problem is a typical example of dynamical programming.
Suppose you have to evaluate an expression like A*B*C*D*E where A,B,C,D and E arematrices. Since matrix multiplication is associative, the order in which multiplications areperformed is arbitrary. However, the number of elementary multiplications neededstrongly depends on the evaluation order you choose.
For example, let A be a 50*10 matrix, B a 10*20 matrix and C a 20*5 matrix.
There are two different strategies to compute A*B*C, namely (A*B)*C and A*(B*C).
The first one takes 15000 elementary multiplications, but the second one only 3500.
Your job is to write a program that determines the number of elementary multiplicationsneeded for a given evaluation strategy.
Input Specification
Input consists of two parts: a list of matrices and a list of expressions.
The first line of the input file contains one integer n (1 <= n <= 26),representing the number of matrices in the first part.The next n lines each contain one capital letter, specifying thename of the matrix, and two integers, specifying the number of rows and columns of the matrix.
The second part of the input file strictly adheres to the following syntax (given in EBNF):
SecondPart = Line { Line } <EOF>
Line = Expression <CR>
Expression = Matrix | "(" Expression Expression ")"
Matrix = "A" | "B" | "C" | ... | "X" | "Y" | "Z"
Output Specification
For each expression found in the second part of the input file, print one line containingthe word "error" if evaluation of the expression leads to an error due to non-matching matrices.Otherwise print one line containing the number of elementary multiplications needed to evaluate the expression in the way specified by the parentheses.
Sample Input
9
A 50 10
B 10 20
C 20 5
D 30 35
E 35 15
F 15 5
G 5 10
H 10 20
I 20 25
A
B
C
(AA)
(AB)
(AC)
(A(BC))
((AB)C)
(((((DE)F)G)H)I)
(D(E(F(G(HI)))))
((D(EF))((GH)I))
Sample Output
0
0
0
error
10000
error
3500
15000
40500
47500
15125
/*************************************************************************
> File Name: 6_3.cpp
> Author:KID_XiaoYuan
> Mail:kuailexy@126.com
> Created Time: 2017年06月05日 星期一 19时25分21秒
************************************************************************/ #include<iostream>
#include<string>
#include<stack> using namespace std; typedef struct Data
{
public:
char name;
int a,b; /*void input (char &name ,int &a , int &b)
{
name = name;
a = a;
b = b;
}*/ }data; int main()
{
data m[];
stack<data> s;
int n;
cin>>n;//输入元素个数
for(int i = ; i < n; i++)//输入元素信息
{
char name;
cin>>name;
cin>>m[name- 'A'].a>>m[name-'A'].b;
}
bool error = false;
string str;//输入计算字符串
cin>>str;
int ans = ;
for(int i =; i < str.length();i++)
{
if(isalpha(str[i]))//判断是否为字母
{
s.push(m[ str[i] - 'A'] );
}
else if( str[i] == ')')//如果是)则需要计算
{
data m1, m2;
m2 = s.top();
s.pop();
m1 = s.top();
s.pop(); if(m1.b != m2.a)
{
printf("ERROR DATA : M1.B = %d M2.A = %d\n",m1.b,m2.a);
error = true;
break;
}
ans += m1.a * m1.b * m2.b;
data c;
c.a = m1.a;
c.b = m2.b;
s.push(c);
} }
error ? (printf("error\n")) : (printf("%d\n",ans));
return ;
}
【C++】c++中栈 队列 的应用的更多相关文章
- C++中 栈和队列的使用方法
C++中 栈和队列已经被封装好,我们使用时只需要按照如下步骤调用即可. 1.包含相关的头文件 包含栈头文件: #include<stack> 包含队列头文件: #include<qu ...
- java 集合 Connection 栈 队列 及一些常用
集合家族图 ---|Collection: 单列集合 ---|List: 有存储顺序 , 可重复 ---|ArrayList: 数组实现 , 查找快 , 增删慢 ---|LinkedList: 链表实 ...
- javascript中的队列结构
1.概念 队列和栈结构不同,栈是一种后进先出的结构,而队列是一种先进先出的结构.队列也是一种表结构,不同的是队列只能在队尾插入元素,在队首删除元素,可以将队列想象成一个在超时等待排队付钱的队伍,或者在 ...
- Java 容器之 Connection栈队列及一些常用
集合家族图 ---|Collection: 单列集合 ---|List: 有存储顺序 , 可重复 ---|ArrayList: 数组实现 , 查找快 , 增删慢 ---|LinkedList: 链表实 ...
- java面向对象的栈 队列 优先级队列的比较
栈 队列 有序队列数据结构的生命周期比那些数据库类型的结构(比如链表,树)要短得多.在程序操作执行期间他们才被创建,通常用他们去执行某项特殊的任务:当完成任务之后,他们就会被销毁.这三个数据结构还有一 ...
- php标准库中QplQueue队列如何使用?
php标准库中QplQueue队列如何使用? 一.总结 1.new对象,然后通过enqueue方法和dequeue方法使用. 二.php标准库中QplQueue队列如何使用? 队列这种数据结构更简单, ...
- 线程回调,线程中的队列,事件,greenlet模块,gevent模块,自定义补丁, 单线程实现并发,协程
1.线程回调 在线程池/进程池每次提交任务,都会返回一个表示任务的对象,Future对象Future对象具备一个绑定方法,add_done_callback 用于指定回调函数 add 意味着可以添加多 ...
- 栈&队列&并查集&哈希表(julyedu网课整理)
date: 2018-11-25 08:31:30 updated: 2018-11-25 08:31:30 栈&队列&并查集&哈希表(julyedu网课整理) 栈和队列 1. ...
- Leetcode栈&队列
Leetcode栈&队列 232.用栈实现队列 题干: 思路: 栈是FILO,队列是FIFO,所以如果要用栈实现队列,目的就是要栈实现一个FIFO的特性. 具体实现方法可以理解为,准备两个栈, ...
随机推荐
- Python complex() 函数
Python complex() 函数 Python 内置函数 描述 complex() 函数用于创建一个值为 real + imag * j 的复数或者转化一个字符串或数为复数.如果第一个参数为字 ...
- Navicat连接mysql(高级选项配置)
.对于服务器上的mysql中存在多个数据库,我们如果全部连接显示,但是平时使用的只有一个库,那么查询的速度会很慢的.所以,今天和大师兄学习了一招.只连接一个自己使用的数据库.配合高级设置,提升很多. ...
- 贪吃蛇Global Java实现(二)
package cn.tcc.snake.util; public class Global {public static final int CELL_SIZE=20;public static f ...
- 写了一个兼容IE9的图片放大器(基于vue)
photoloupe 图片放大器 第一次写vue插件,本人比较喜欢用简单易懂的写法,不喜勿喷. 本插件支持IE9及以上版本,已经过验证. 本插件可根据需要设置放大倍数,最小支持1倍,支持小数 下载地址 ...
- java中关于null的一些理解
1.null是Java中的关键字,像public.static.final.它是大小写敏感的,你不能将null写成Null或NULL,编译器将不能识别它们然后报错. 2.null是任何引用类型的默认值 ...
- js实现各种复制到剪贴板的方法
一.实现点击按钮,复制文本框中的的内容 <script type="text/javascript"> function copyUrl2() { var Url2=d ...
- JFinal上传文件时用getFile()方法报错
原因是缺少cos.jar包,补上即可.
- gcc编译器如何生成指定的文件名
放哪里都可以,进到文件所放路径,用gcc编译器编译.如:gcc -o test test.c 就是说把test.c文件编译链接生成test可执行程序.在linux下生成test.out,然后再命令:. ...
- Linux_(2)基本命令(下)
六.文件搜索命令11 :which功能描述:显示系统命令所在目录命令所在路径:/usr/bin/which执行权限:所有用户语法:which [命令名称]范例:$ which ls 12 :find功 ...
- Oracle_SQL(6) 单行函数
一.单行函数1.定义:对表或视图的查询时,针对每行记录返回一个值的函数.2.用途:用于select语句,where条件3.分类: 数值函数 Number Functions 字符函数(返回字符) Ch ...