UVA 11995 I Can Guess the Data Structure! (STL应用)

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- le (EOF).
Output
For each test case, output one of the following:
stack It's de nitely a stack.
queue It's de nitely a queue.
priority queue It's de nitely 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 men-
tioned 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
Sample Output
queue
not sure
impossible
stack
priority queue

 

题目大意:

就是说,给你一些操作,1 x表示把x放入到某种数据结构当中,2 x表示取出来他最前面的元素是x,问你这些操作过后,你能不能猜出来他是哪种数据结构?

解题思路:

水题,直接模拟就好了。

注意复习简单的STL,注意stack的push,pop  queue的push,pop  priority_queue的push和pop,

stack  s.top,

queue s.front

priority_queue  s.top

 

代码:

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

using namespace std;

# define MAX 100004

int n;
int a[MAX],t[MAX];
int check_stack()
{
    stack<int>s;
    for ( int i = 0;i < n;i++ )
    {
        if (t[i]==2)
        {
            if (s.empty())
                return 0;
            int x = s.top();
            s.pop();
            if ( x!=a[i] )
                return 0;
        }
        else
            s.push(a[i]);
    }
    return 1;
}

int check_queue()
{
    queue<int>Q;
    for ( int i = 0;i < n;i++ )
    {
        if ( t[i]==2 )
        {
            if (Q.empty())
                return 0;
            int x = Q.front();
            Q.pop();
            if ( x!=a[i] )
                return 0;
        }
        else
            Q.push(a[i]);
    }
    return 1;
}

int check_pq()
{
    priority_queue<int>PQ;
    for ( int i = 0;i < n;i++ )
    {
        if ( t[i]==2 )
        {
            if (PQ.empty())
                return 0;
            int x = PQ.top();
            PQ.pop();
            if (x!=a[i])
                return 0;
        }
        else
            PQ.push(a[i]);
    }
    return 1;
}


int main(void)
{
    while ( scanf("%d",&n)!=EOF )
    {
        for ( int i = 0;i < n;i++ )
            scanf("%d%d",&t[i],&a[i]);
        int cnt1 = check_stack();
        int cnt2 = check_queue();
        int cnt3 = check_pq();
        if (cnt1==0&&cnt2==0&&cnt3==0)
            puts("impossible");
        else if (cnt1==1&&cnt2==0&&cnt3==0)
            puts("stack");
        else if (cnt1==0&&cnt2==1&&cnt3==0)
            puts("queue");
        else if (cnt1==0&&cnt2==0&&cnt3==1)
            puts("priority queue");
        else
            puts("not sure");

    }


    return 0;
}

 

posted @ 2015-08-19 14:12  BYYB_0506  阅读(171)  评论(0编辑  收藏  举报