HDU 1702 - ACboy needs your help again!

ACboy was kidnapped!!
he miss his mother very much and is very scare now.You can't image how dark the room he was put into is, so poor :(.
As a smart ACMer, you want to get ACboy out of the monster's labyrinth.But when you arrive at the gate of the maze, the monste say :" I have heard that you are very clever, but if can't solve my problems, you will die with ACboy."
The problems of the monster is shown on the wall:
Each problem's first line is a integer N(the number of commands), and a word "FIFO" or "FILO".(you are very happy because you know "FIFO" stands for "First In First Out", and "FILO" means "First In Last Out").
and the following N lines, each line is "IN M" or "OUT", (M represent a integer).
and the answer of a problem is a passowrd of a door, so if you want to rescue ACboy, answer the problem carefully!

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

题意:栈和队列的模拟,栈(先进后出),队列(先进先出)

#include<iostream>
#include<cstdio>
#include<queue>
#include<stack>

using namespace std;

int t, n;
void Stack() {
    char c[5];
    int x;
    stack<int> s;
    for (int  i = 0; i < n; i++)
    {
        scanf("%s", c);
        if (!strcmp(c,"IN"))
        {
            scanf("%d", &x);
            s.push(x);

        }
        else if (!s.empty())
        {
            printf("%d\n", s.top());
            s.pop();
        }
        else printf("None\n");
    }

}

void Queue() {
    char c[5];
    int x;
    queue<int>q;
    for (int i = 0; i < n; i++)
    {
        scanf("%s", c);
        if (!strcmp(c,"IN"))
        {
            scanf("%d", &x);
            q.push(x);
        }
        else if (!q.empty())
        {
            printf("%d\n", q.front());
            q.pop();
        }
        else printf("None\n");
    }
}
int main() {
    char c[10];
    scanf("%d", &t);
    while (t--)
    {
        scanf("%d%s", &n, c);
        if (!strcmp(c, "FIFO")) Queue();
        else Stack();

    }
    return 0;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 10,797评论 0 23
  • 很久以前 大概19岁 痴迷过安妮宝贝的文字 如今都29了 那些故事也忘得差不多了 七月与安生是我比较喜欢的故事之一...
    仟维阅读 305评论 0 1
  • 我所有的自负皆来自我的自卑,所有的英雄气概都来自于我的软弱。嘴里振振有词是因为心里满是怀疑,深情是因为痛恨自己无情...
    Gapeng阅读 11,089评论 0 5
  • 1. 这是一篇不正式的书评。 写给我最为敬爱的活着的中国作家,阿丁。 他的新书《职业撒谎者的供述》发布会,我去了,...
    某人李下阅读 1,896评论 13 28
  • (一) 正值花开,争相斗妍。 “真香啊!” 他嗅了嗅。 “是啊!” 她指了指一片花海。 “我说的是你。” 俩人相视...
    发芽的种子娜阅读 507评论 8 13

友情链接更多精彩内容