寒假训练营第三天(基础数据结构1)B题: I Can Guess the Data Structure!(建议使用栈、队列和优先队列来模拟)

B:I Can Guess the Data Structure!(建议使用栈、队列和优先队列来模拟)

There is a bag-like data structure, supporting two operations:

1 x
Throw an element x into the bag.

2
Take out an element from the bag.

Given a sequence of operations with return values, you’re going to guess the data structure. It is a stack (Last-In, First-Out), a queue (First-In, First-Out), a priority-queue (Always take out larger elements first) or something else that you can hardly imagine!

Input
There are several test cases. Each test case begins with a line containing a single integer n (1<=n<=1000). Each of the next n lines is either a type-1 command, or an integer 2 followed by an integer x. That means after executing a type-2 command, we get an element x without error. The value of x is always a positive integer not larger than 100. The input is terminated by end-of-file (EOF). The size of input file does not exceed 1MB.

Output
For each test case, output one of the following:

stack
It’s definitely a stack.

queue
It’s definitely a queue.

priority queue
It’s definitely a priority queue.

impossible
It can’t be a stack, a queue or a priority queue.

not sure
It can be more than one of the three data structures mentioned above.

扫描二维码关注公众号,回复: 5070048 查看本文章

Sample Input
6
1 1
1 2
1 3
2 1
2 2
2 3
6
1 1
1 2
1 3
2 3
2 2
2 1
2
1 1
2 2
4
1 2
1 1
2 1
2 2
7
1 2
1 5
1 1
1 3
2 5
1 4
2 4

Output for the Sample Input
queue
not sure
impossible
stack
priority queue

题意:这道题就是通过输入若干数据结构类型,让你来判断,理解题意后并不难,但在规定时间并没有做出来,在补题时,通过学长给的建议有了点思路

首先开始我的代码如下:很遗憾结果时间超限

#include<iostream>
#include<cstdio>
#include<stack>
#include<queue>
using namespace std;
int main()
{
    int n;
    while(scanf("%d",&n))
    {
        stack<int>x;
        queue<int>y;
        priority_queue<int>z;
        int p,q,i,j,f[3];
        for(i=0;i<3;i++)
        {
            f[i]=1;
        }
        for(i=0;i<n;i++)
        {
            scanf("%d %d",&p,&q);
            if(p==1)
            {
                 x.push(q);
                 y.push(q);
                 z.push(q);
            }
            else
            {
                if(!x.empty())
                {
                    if(x.top()!=q)                         //多次判断导致时间超限
                    {
                        f[0]=0;
                    }
                    x.pop();
                }
                else
                    f[0]=0;
                if(!y.empty())
                {
                    if(y.front()!=q)                            //多次判断导致时间超限
                        f[1]=0;
                    y.pop();
                }
                else
                    f[1]=0;
                if(!z.empty())
                {
                    if(z.top()!=q)                                     //多次判断导致时间超限
                        f[2]=0;
                    z.pop();
                }
                else
                    f[2]=0;

            }
        }
        int sum=0;
        for(i=0;i<3;i++)
        {
            if(f[i]==1)
                sum++;
        }
        if(sum==0)
            printf("impossible\n");
        else if(sum>1)
            printf("not sure\n");
        else
        {
            if(f[0]==1)
                printf("stack\n");
            if(f[1]==1)
                printf("queue\n");
            if(f[2]==1)
                printf("priority queue\n");
        }
    }
    return 0;
}

但在后面自己通过不断修改和寻问学长,解决了这个问题,最后***AC***代码如下:

#include<iostream>
#include<cstdio>
#include<stack>
#include<queue>
using namespace std;
int main()
{
    int n,i;
    while(scanf("%d",&n)!=EOF)
    {
        stack<int>x;
        queue<int>y;
        priority_queue<int>z;
        int f[3];
        for(i=0;i<3;i++)
        {
            f[i]=1;
        }
        for(i=0;i<n;i++)
        {
            int p,q;
            scanf("%d%d",&p,&q);
            if(p==1)
            {
                 x.push(q);
                 y.push(q);
                 z.push(q);
            }
            else
            {
                if(!x.empty())
                {
                    int a=x.top();
                    if(a!=q)
                    {
                        f[0]=0;
                    }
                    x.pop();
                }
                else
                    f[0]=0;
                if(!y.empty())
                {
                    int b=y.front();
                    if(b!=q)
                        f[1]=0;
                    y.pop();
                }
                else
                    f[1]=0;
                if(!z.empty())
                {
                    int c=z.top();
                    if(c!=q)
                        f[2]=0;
                    z.pop();
                }
                else
                    f[2]=0;

            }
        }
        int sum=0;
        for(i=0;i<3;i++)
        {
            if(f[i]==1)
                sum++;
        }
        if(sum==0)
            printf("impossible\n");
        else if(sum>1)
            printf("not sure\n");
        else
        {
            if(f[0]==1)
                printf("stack\n");
            else if(f[1]==1)
                printf("queue\n");
            else if(f[2]==1)
                printf("priority queue\n");
        }
    }
    return 0;
}

小白还在不断努力

猜你喜欢

转载自blog.csdn.net/boliu147258/article/details/86633399