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

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

题意:

        现在有一个数据结构s,对应两种操作: 1 x表示存放x入s. 2 x表示从s中无错的取出了x.现在要你判断s的类型是:栈,队列还是优先队列(数值大的先出),不确定或者不可能.

分析:

        刘汝佳:训练指南P186例题.

        由于STL已经封装了这3种数据结构,我们只要分别建立这3种数据结构模拟操作一遍即可.看看对应的stack或queue或priority_queue进行指令1或2的结果 是否 完全符合 指令2的所有结果。

        最终整合3种结构的判断结果输出即可。

AC代码(新):

#include<vector>
#include<cstdio>
#include<stack>
#include<queue>
using namespace std;

struct Data_Struct
{
    //vc存放指令pair序列
    vector<pair<int,int> > vc;

    //构造函数
    Data_Struct(int n)
    {
        for(int i=0;i<n;i++)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            vc.push_back(make_pair(x,y));
        }
    }

    //判断是否可能是一个栈
    bool is_stack()
    {
        stack<int> S;
        for(int i=0;i<vc.size();i++)
        {
            int x=vc[i].first, y=vc[i].second;
            if(x==1) S.push(y);
            else if(x==2)
            {
                if(S.empty() || S.top()!=y ) return false;
                S.pop();
            }
        }
        return true;
    }

    //判断是否可能是一个队列
    bool is_queue()
    {
        queue<int> Q;
        for(int i=0;i<vc.size();i++)
        {
            int x=vc[i].first, y=vc[i].second;
            if(x==1) Q.push(y);
            else if(x==2)
            {
                if(Q.empty() || Q.front()!=y ) return false;
                Q.pop();
            }
        }
        return true;
    }

    //判断是否可能是一个优先队列
    bool is_priority_queue()
    {
        priority_queue<int> Q;
        for(int i=0;i<vc.size();i++)
        {
            int x=vc[i].first,y=vc[i].second;
            if(x==1) Q.push(y);
            else
            {
                if(Q.empty() || Q.top()!=y )return false;
                Q.pop();
            }
        }
        return true;
    }

    //输出整合后的结果
    void solve()
    {
        int S=is_stack(), Q=is_queue(), PQ=is_priority_queue();

        if(S+Q+PQ==0) printf("impossible\n");
        else if(S+Q+PQ==1)
        {
            if(S) printf("stack\n");
            else if(Q) printf("queue\n");
            else if(PQ) printf("priority queue\n");
        }
        else if(S+Q+PQ>1)
            printf("not sure\n");
    }
};

int main()
{
    int n;
    while(scanf("%d",&n)==1)
    {
        Data_Struct ds(n);

        ds.solve();
    }
    return 0;
}

AC代码:

#include<iostream>
#include<cstdio>
#include<queue>
#include<stack>
using namespace std;
const int maxn=1000+100;
struct command
{
    int type;
    int x;
}coms[maxn];
int main()
{
    int n;
    while(scanf("%d",&n)==1)
    {
        queue<int> q;
        priority_queue<int> pq;
        stack<int> s;
        bool ok1=true,ok2=true,ok3=true;
        for(int i=0;i<n;i++)
            scanf("%d%d",&coms[i].type,&coms[i].x);
        for(int i=0;i<n;i++)
        {
            if(!ok1 && !ok2 && !ok3) break;
            int t=coms[i].type,x=coms[i].x;
            if(t==1)
            {
                if(ok1) q.push(x);
                if(ok2) pq.push(x);
                if(ok3) s.push(x);
            }
            else if(t==2)
            {
                if(ok1)
                {
                    if(q.empty())ok1=false;
                    else
                    {
                        int y=q.front();q.pop();
                        if(y!=x) ok1=false;
                    }
                }
                if(ok2)
                {
                    if(pq.empty())ok2=false;
                    else
                    {
                        int y=pq.top();pq.pop();
                        if(y!=x) ok2=false;
                    }
                }
                if(ok3)
                {
                    if(s.empty())ok3=false;
                    else
                    {
                        int y=s.top();s.pop();
                        if(y!=x) ok3=false;
                    }
                }
            }
        }
        if(ok1 && !ok2 && !ok3) printf("queue\n");
        else if(!ok1 && ok2 && !ok3) printf("priority queue\n");
        else if(!ok1 && !ok2 && ok3) printf("stack\n");
        else if(!ok1 && !ok2 && !ok3) printf("impossible\n");
        else printf("not sure\n");
    }
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
STL(Standard Template Library)是C++语言中的一个标准库,提供了许多数据结构和算法,包括向量、链表、栈、队列、堆、集合、映射、排序、查找等。使用STL可以大大提高程序的效率和可读性,因为它已经封装好了很多常用的数据结构和算法,可以直接拿来使用,而且这些封装好的数据结构和算法都是经过优化的,效率也很高。 下面是几个常用的STL应用示例: 1. 使用向量存储一组数据,并遍历输出: ```c++ #include <vector> #include <iostream> int main() { std::vector<int> v = {1, 2, 3, 4, 5}; for (auto i : v) { std::cout << i << " "; } return 0; } ``` 2. 使用集合存储一组数据,并遍历输出: ```c++ #include <set> #include <iostream> int main() { std::set<int> s = {1, 2, 3, 4, 5}; for (auto i : s) { std::cout << i << " "; } return 0; } ``` 3. 使用映射存储一组键值对,并遍历输出: ```c++ #include <map> #include <iostream> int main() { std::map<std::string, int> m = {{"a", 1}, {"b", 2}, {"c", 3}}; for (auto i : m) { std::cout << i.first << " " << i.second << std::endl; } return 0; } ``` 4. 使用迭代器遍历一个容器: ```c++ #include <vector> #include <iostream> int main() { std::vector<int> v = {1, 2, 3, 4, 5}; for (auto it = v.begin(); it != v.end(); ++it) { std::cout << *it << " "; } return 0; } ``` 5. 使用STL算法对容器中的数据进行排序: ```c++ #include <vector> #include <algorithm> #include <iostream> int main() { std::vector<int> v = {5, 4, 3, 2, 1}; std::sort(v.begin(), v.end()); for (auto i : v) { std::cout << i << " "; } return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值