栈和队列简单例题

ACboy needs your help again!

Input
The input contains multiple test cases. The first line has one integer,represent the number oftest cases. And the input of each subproblem are described above.

Output
For each command “OUT”, you should output a integer depend on the word is “FIFO” or “FILO”, or a word “None” if you don’t have any integer.

Sample Input
4
4 FIFO
IN 1
IN 2
OUT
OUT
4 FILO
IN 1
IN 2
OUT
OUT
5 FIFO
IN 1
IN 2
OUT
OUT
OUT
5 FILO
IN 1
IN 2
OUT
IN 3
OUT

Sample Output
1
2
2
1
1
2
None
2
3
分析:
根据题意就可以知道需要使用队列和栈的知识,FIFO是队列的特征,FILO是栈的特征。
我们可以手写模拟队列和栈,也可以直接使用自带的库函数queue和stack。
这里我就是用的queue和stack
代码如下:

#include<stdio.h>
#include<iostream>
using namespace std;
#include<queue>
#include<stack>
int main()
{
    int n;
    cin>>n;
    while(n--)
    {
        int x;
        cin>>x;
        string s;
        cin>>s;
        if(s=="FIFO")
        {
            string op;
            queue<int> q;
            int e;
            while(x--)
            {
                cin>>op;
                if(op=="IN"){
                    cin>>e;
                    q.push(e);
                }else if(op=="OUT"){
                if(q.empty())cout<<"None"<<endl;
                else{ cout<<q.front()<<endl;q.pop();}
                }

            }
        }
        else if(s=="FILO")
        {
            string op;
            stack<int> sta;
            int e;
            while(x--)
            {

                cin>>op;
                if(op=="IN")
                {
                    cin>>e;
                    sta.push(e);
                }
                else if(op=="OUT")
                {
                    if(sta.empty())cout<<"None"<<endl;
                    else {cout<<sta.top()<<endl;sta.pop();}
                }
            }
        }
    }
    return 0;
}

该题目相对简单。

A Stack or A Queue?

Input
There are multiple test cases. The first line of input contains an integer T (T <= 100), indicating the number of test cases. Then T test cases follow.
Each test case contains 3 lines: The first line of each test case contains only one integer N indicating the number of integers (1 <= N <= 100). The second line of each test case contains N integers separated by a space, which are given in the order of going into the structure (that is, the first one is the earliest going in). The third line of each test case also contains N integers separated by a space, whick are given in the order of coming out of the structure (the first one is the earliest coming out).
Output
For each test case, output your guess in a single line. If the structure can only be a stack, output “stack”; or if the structure can only be a queue, output “queue”; otherwise if the structure can be either a stack or a queue, output “both”, or else otherwise output “neither”.
Sample Input

4
3
1 2 3
3 2 1
3
1 2 3
1 2 3
3
1 2 1
1 2 1
3
1 2 3
2 3 1

Sample Output

stack
queue
both
neither
分析:
根据题意可知该题也是考察简单的栈和队列的知识,但是是反过来考察我们对栈和队列的判断。
了解栈和队列的特点,这道题很快就能AC.
#include<stdio.h>
#include
using namespace std;
#include
#include
int main()
{
int n;
cin>>n;
while(n–)
{
int x;
cin>>x;
int y=x;
queue que;
stack sta;
int e,flag1=0,flag2=0;//计数器,下面用来判断数据相同的个数来判断是否为栈或者队列
while(x–)
{
cin>>e;
que.push(e);
sta.push(e);//分别用队列和栈来储存输入的数据方便对照
}
for(int i=0;i<y;i++)
{
cin>>x;
if(que.front()x){flag1++;que.pop();}
if(sta.top()x){flag2++;sta.pop();}
}
if(flag1
y&&flag2
y)cout<<“both”<<endl;
else if(flag1y)cout<<“queue”<<endl;
else if(flag2
y)cout<<“stack”<<endl;
else cout<<“neither”<<endl;
}
}
这两题都是栈和队列最基础的题,直接可以使用函数库中的queue 和stack。

愚人节礼物

Problem Description
四月一日快到了,Vayko想了个愚人的好办法——送礼物。嘿嘿,不要想的太好,这礼物可没那么简单,Vayko为了愚人,准备了一堆盒子,其中有一个盒子里面装了礼物。盒子里面可以再放零个或者多个盒子。假设放礼物的盒子里不再放其他盒子。

用()表示一个盒子,B表示礼物,Vayko想让你帮她算出愚人指数,即最少需要拆多少个盒子才能拿到礼物。

Input
本题目包含多组测试,请处理到文件结束。 每组测试包含一个长度不大于1000,只包含’(’,’)'和’B’三种字符的字符串,代表Vayko设计的礼物透视图。 你可以假设,每个透视图画的都是合法的。

Output
对于每组测试,请在一行里面输出愚人指数。

Sample Input
((((B)()))())
(B)

Sample Output
4
1
分析:
这道题相对来说比较有趣,第一眼看上去觉得有点蒙,后来仔细看一下发现也是很简单的一道题。
用数组或者队列就可以解出该题。

#include<stdio.h>
#include<iostream>
using namespace std;
#include<queue>
#include<stack>
#include<string.h>
int main()
{   char c[1007];
    while(scanf("%s",c)!=EOF)//遇到文件结束
    {//c这样的输入居然不需要初始化
        queue<char> que;

        for(int i=0;i<strlen(c);i++)
        {
            que.push(c[i]);
        }
        int flag=0;
        while(que.front()!='B')//遇到B结束
        {
            if(que.front()=='(')flag++;
            if(que.front()==')')flag--;
            que.pop();
        }
        cout<<flag<<endl;
    }

}

总结:

queue函数
创建队列时需要确定数列中的元素类型,例如queueque;
queue.pop()只是从队列中将队头元素删除掉,并不会返回队头元素。
queue.front()可以得到队头元素。
queue.empty()判断队列是否为空。
queue.size()队列元素个数。
stack函数
和queue函数差不多
需要给出元素类型
stack.pop()会删除栈顶元素,但不会返回元素的值。
stack.top()返回栈顶元素(最大的差别就是这里)
stack.empty()、stack.size()都类似。

今天下午遇到了一道比较难的题贴出来,写了好久没写出来,贴出来回来补解叭。

Problem Description

As the new term comes, the Ignatius Train Station is very busy nowadays. A lot of student want to get back to school by train(because the trains in the Ignatius Train Station is the fastest all over the world v). But here comes a problem, there is only one railway where all the trains stop. So all the trains come in from one side and get out from the other side. For this problem, if train A gets into the railway first, and then train B gets into the railway before train A leaves, train A can’t leave until train B leaves. The pictures below figure out the problem. Now the problem for you is, there are at most 9 trains in the station, all the trains has an ID(numbered from 1 to n), the trains get into the railway in an order O1, your task is to determine whether the trains can get out in an order O2.

Input
The input contains several test cases. Each test case consists of an integer, the number of trains, and two strings, the order of the trains come in:O1, and the order of the trains leave:O2. The input is terminated by the end of file. More details in the Sample Input.

Output
The output contains a string “No.” if you can’t exchange O2 to O1, or you should output a line contains “Yes.”, and then output your way in exchanging the order(you should output “in” for a train getting into the railway, and “out” for a train getting out of the railway). Print a line contains “FINISH” after each test case. More details in the Sample Output.

Sample Input
3 123 321
3 123 312

Sample Output
Yes.
in
in
in
out
out
out
FINISH
No.
FINISH

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值