UVA 11995 I Can Guess the Data Structure!

本文介绍了如何通过输入的序列判断数据操作对应的数据结构类型,涉及栈、队列和优先队列的应用。通过实例代码演示了如何使用STL库中的容器来解决该问题,并关注于边界条件的处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题意:给出若干组数据,每组数据包含t行,每行第一个数为1或2;1表示进入,2表示取出,根据出入顺序判断为何种数据结构

链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=18700

思路:需要判断的数据结构有三种,栈,队列,优先队列,即单纯的STL应用,判断每次取出是否与三种数据结构中的情况相等即可。

注意点:可能STL中为空的情况下继续取出,需要判断边界情况。


以下为AC代码:

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <vector>
#include <deque>
#include <list>
#include <cctype>
#include <algorithm>
#include <climits>
#include <queue>
#include <stack>
#include <cmath>
#include <map>
#include <set>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#define ll long long
#define ull unsigned long long
#define all(x) (x).begin(), (x).end()
#define clr(a, v) memset( a , v , sizeof(a) )
#define pb push_back
#define mp make_pair
#define read(f) freopen(f, "r", stdin)
#define write(f) freopen(f, "w", stdout)
using namespace std;

int main()
{
    ios::sync_with_stdio( false );
    queue<int> q;
    stack<int> s;
    priority_queue<int> pq;
    int t;
    while ( cin >> t )
    {
        int a, b;
        bool f1 = 1;
        bool f2 = 1;
        bool f3 = 1;
        while ( !  q.empty() )  q.pop();
        while ( !  s.empty() )  s.pop();
        while ( ! pq.empty() ) pq.pop();
        for ( int i = 0; i < t; i ++ )
        {
            cin >> a >> b;
            if ( a == 1 )
            {
                q.push( b );
                s.push( b );
                pq.push( b );
            }
            else if ( a == 2 && ! q.empty() )
            {
                if ( q.front() != b ) f1 = 0; q.pop();
                if ( s.top() != b ) f2 = 0; s.pop();
                if ( pq.top() != b ) f3 = 0; pq.pop();
            }
            else if ( a == 2 && q.empty() )
            {
                f1 = 0;
                f2 = 0;
                f3 = 0;
            }
        }
        if      (     f1   && ( ! f2 ) && ( ! f3 ) ) cout << "queue" << endl;
        else if (     f2   && ( ! f1 ) && ( ! f3 ) ) cout << "stack" << endl;
        else if (     f3   && ( ! f1 ) && ( ! f2 ) ) cout << "priority queue" << endl;
        else if ( ( ! f1 ) && ( ! f2 ) && ( ! f3 ) ) cout << "impossible" << endl;
        else cout << "not sure" << endl;
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值