uva - 11995 - I Can Guess the Data Structure!

题意:输入一个数n,接着输入n行数,第行数由2个正整数构成,其中第一个整数要么为1,要么为2,若为1,则将1后面的整数放进一个包里;若为2,则从包里取出一个整数,最后根据放入包里与从包里取出的数的顺序判断这个包是个什么样的数据结构。

题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=229&page=show_problem&problem=3146

——>>这本来是一道不难的题目,可是一个不留神却折腾了半天,如果包里数为空,但无错地从包里取出一些数的时候,结果应该是impossible……

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

using namespace std;

int main()
{
    int n, a, b, i;
    while(cin>>n)
    {
        queue<int> qu;      //声明队列
        priority_queue<int> pri_qu;     //声明优先队列
        stack<int> st;      //声明栈
        bool qu_ok = 1, pri_qu_ok = 1, st_ok = 1;       //各数据结构的成立标记
        for(i = 0; i < n; i++)
        {
            cin>>a>>b;
            if(a == 1)
            {
                qu.push(b);     //入列
                pri_qu.push(b);     //入列
                st.push(b);     //进栈
            }
            else
            {
                if(qu.empty() || pri_qu.empty() || st.empty())      //如果任意一个数据结构空了还输出,一定没一个数据结构符合
                {
                    qu_ok = 0;
                    pri_qu_ok = 0;
                    st_ok = 0;
                }
                if(qu_ok == 1)       //判断是否可为队列
                {
                    int temp_qu = qu.front();
                    qu.pop();
                    if(temp_qu != b) qu_ok = 0;     //若不能为队列,修改标记
                }
                if(pri_qu_ok == 1)       //判断是否可为优先队列
                {
                    int temp_pri_qu = pri_qu.top();
                    pri_qu.pop();
                    if(temp_pri_qu != b) pri_qu_ok = 0;     //若不能为优先队列,修改标记
                }
                if(st_ok == 1)       //判断是否可为栈
                {
                    int temp_st = st.top();
                    st.pop();
                    if(temp_st != b) st_ok = 0;     //若不能为栈,修改标记
                }
            }
        }
        int cnt = 0;        //满足的数据结构记数器
        if(qu_ok) cnt++;
        if(pri_qu_ok) cnt++;
        if(st_ok) cnt++;

        if(cnt == 0) cout<<"impossible"<<endl;
        else if(cnt == 1)
        {
            if(qu_ok) cout<<"queue"<<endl;
            else if(pri_qu_ok) cout<<"priority queue"<<endl;
            else cout<<"stack"<<endl;
        }
        else cout<<"not sure"<<endl;
    }

    return 0;
}



posted @ 2013-01-02 19:05  xiaodanding  阅读(143)  评论(0编辑  收藏  举报