sdutacm-数据结构实验之栈八:栈的基本操作

TimeLimit: 1000MS MemoryLimit: 65536KB

SubmitStatistic

Problem Description

堆栈是一种基本的数据结构。堆栈具有两种基本操作方式,push poppush一个值会将其压入栈顶,而 pop 则会将栈顶的值弹出。现在我们就来验证一下堆栈的使用。

Input

首先输入整数t1 <= t <= 10),代表测试的组数,以后是 t 组输入。
 
对于每组测试数据,第一行输入两个正整数 m1 <= m <= 100)n1 <= n <= 1000),其中m代表当前栈的最大长度,n代表本组测试下面要输入的操作数。 而后的 n 行,每行的第一个字符可能是'P’或者'O’或者'A’;如果是'P’,后面还会跟着一个整数,表示把这个数据压入堆栈;如果是'O’,表示栈顶元素出栈;如果是'A',表示询问当前栈顶的值'

Output

 对于每组测试数据,根据其中的命令字符来处理堆栈;
1)对所有的'P'操作,如果栈满输出'F',否则完成压栈操作;
2)对所有的'A'操作,如果栈空,则输出'E',否则输出当时栈顶的值;
3)对所有的'O'操作,如果栈空,则输出'E',否则输出栈顶元素的值,并让其出栈;
每个输出占据一行,每组测试数据(最后一组除外)完成后,输出一个空行。

Example Input

2

5 10

A

P 9

A

P 6

P 3

P 10

P 8

A

P 2

O

2 5

P 1

P 3

O

P 5

A

Example Output

E

9

8

F

8

 

3

5

Hint

建议:用串的方式(%s)读入操作字符。

Author

#include<stdio.h>

#include<string.h>

#include<math.h>

#include<algorithm>

#include<stdlib.h>

#include<stack>

using namespace std;

bool p(int b,int m)

{

  if(b<m)

  return false;

  else

  return true;

 

 

}

int main()

{

    int t;

    scanf("%d",&t);

 

 while(t--)

 {

     int m,n,k;

     scanf("%d%d",&m,&n);

     char t[10];

     stack<int>u;

     for(int i=1;i<=n;i++)

     {

 

        scanf("%s",t);

        if(t[0]=='P')

        {

          scanf("%d",&k);

          if(p(u.size(),m))

          {

             printf("F\n");

          }

          else

          {

              u.push(k);

 

          }

        }

        if(t[0]=='A')

        {

           if(u.empty())

           {

              printf("E\n");

          }

           else

           {

             printf("%d\n",u.top());

 

           }

        }

            if(t[0]=='O')

            {

                if(u.empty())

                {

                    printf("E\n");

                }

                else

                {

                 printf("%d\n",u.top());

                  u.pop();

                }

 

            }

 

 

 

     }

 

     printf("\n");

 

 

 }

 

 

 

return 0;

}

 

 

 

 

/***************************************************

User name: jk160505徐红博

Result: Accepted

Take time: 0ms

Take Memory: 124KB

Submit time: 2017-01-13 10:32:41

****************************************************/