1. 程式人生 > >B - I Can Guess the Data Structure!(建議使用棧、隊列和優先隊列來模擬)

B - I Can Guess the Data Structure!(建議使用棧、隊列和優先隊列來模擬)

enc 優先 ont ron con 元素 sample pre win

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, youre 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 Its definitely a stack. queue Its definitely a queue. priority queue Its definitely a priority queue.
impossible It cant be a stack, a queue or a priority queue. not sure It can be more than one of the three data structures mentioned above. 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 <queue>
#include <stack>
#include<cstdio>


using namespace std;

int main()
{
    int n;
    int i,x,y;
    while(cin>>n)
    {
        stack<int>s;
        queue<int>q;
        priority_queue<int>pq;


        int flag=0;     //判斷輸入符合幾種情況
        int a[3]={1,1,1};   //三個數組元素,分別對應三種數據結構
        for(i=0;i<n;i++)
        {
            cin>>x>>y;
            if(x==1)     //將輸入的數字分別放進三種數據結構裏
            {
                s.push(y);
                q.push(y);
                pq.push(y);
            }
            else    //x=2的情況,也就是將每種數據結構元素拿出來和輸入的數進行比較
            {
                if(!q.empty()&&a[0]==1)  //不為空且a=1
                {
                    int b=q.front();   //判斷
                    q.pop();
                    if(y!=b)   //不相同,把a置零反之a=1
                        a[0]=0;
                }
                else a[0]=0;  //為空的情況a也是等於1


                if(!s.empty()&&a[1]==1)
                {
                    int b=s.top();
                    s.pop();
                    if(y!=b)
                        a[1]=0;
                }
                else a[1]=0;


                if(!pq.empty()&&a[2]==1)
                {
                    int b=pq.top();
                    pq.pop();
                    if(y!=b)
                        a[2]=0;
                }
                else a[2]=0;


            }
        }


            for(i=0;i<3;i++)  //計算有多少種數據結構滿足輸入條件
            {
                if(a[i]==1)
                    flag++;
            }



        if(flag==0)
                cout<<"impossible"<<endl;
            else if(flag==1)
            {
                if(a[0]==1)
                    cout<<"queue"<<endl;
                else if(a[1]==1)
                    cout<<"stack"<<endl;
                else if(a[2]==1)
                    cout<<"priority queue"<<endl;
            }
            else if(flag>1)
                cout<<"not sure"<<endl;
    }
    return 0;
}

大概思路:1的數字輸入完之後,進入2數字判斷,y與每種數據結構出來的元素進行判斷。

順,別忘記判斷為空的情況

B - I Can Guess the Data Structure!(建議使用棧、隊列和優先隊列來模擬)