c_cpp uva11995

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp uva11995相关的知识,希望对你有一定的参考价值。

#include <iostream>
#include <memory>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;

template <typename DataStruct>
static void candidate_push(DataStruct &ds, const typename DataStruct::value_type &value)
{
    ds.push(value);
}

template <typename DataStruct>
static typename DataStruct::value_type candidate_pop(DataStruct &ds)
{
    typename DataStruct::value_type value = ds.top();
    ds.pop();
    return value;
}

template <typename T>
static T candidate_pop(queue<T> &ds)
{
    T value = ds.front();
    ds.pop();
    return value;
}

template <typename T>
class tester
{
public:
    class candidate_base
    {
    public:
        candidate_base(const string &name) : m_name(name), m_possible(true)
        {
        }

        const string &get_name() const
        {
            return m_name;
        }

        bool is_possible() const
        {
            return m_possible;
        }

        void push(const T &value)
        {
            do_push(value);
        }

        void pop(const T &expect_value)
        {
            try
            {
                if (do_pop() != expect_value) m_possible = false;
            }
            catch (...)
            {
                m_possible = false;
            }
        }

    private:
        virtual void do_push(const T &value) = 0;
        virtual T do_pop() = 0;

    private:
        string m_name;
        bool m_possible;
    };

public:
    template <typename DataStruct>
    class candidate : public candidate_base
    {
    public:
        candidate(const string &name) : candidate_base(name)
        {
        }

    private:
        virtual void do_push(const T &value) override
        {
            candidate_push(m_ds, value);
        }

        virtual T do_pop() override
        {
            if (m_ds.empty()) throw runtime_error("underflow");

            return candidate_pop(m_ds);
        }

    private:
        DataStruct m_ds;
    };

public:
    tester() : m_candidates(init_candidates())
    {
    }

    void push(const T &value)
    {
        for (auto &c : m_candidates) c->push(value);
    }

    void pop(const T &expect_value)
    {
        for (auto &c : m_candidates) c->pop(expect_value);
    }

    string result() const
    {
        shared_ptr<candidate_base> ptr = nullptr;
        size_t count = 0;

        for (const auto &c : m_candidates)
        {
            if (c->is_possible())
            {
                ptr = c;
                count++;
            }
        }

        switch (count)
        {
        case 0:
            return "impossible";
        case 1:
            return ptr->get_name();
        default:
            return "not sure";
        }
    }

private:
    static vector<shared_ptr<candidate_base>> init_candidates()
    {
        return
        {
            make_shared<candidate<stack<T>>>("stack"),
            make_shared<candidate<queue<T>>>("queue"),
            make_shared<candidate<priority_queue<T>>>("priority queue"),
        };
    }

private:
    vector<shared_ptr<candidate_base>> m_candidates;
};

void test_case(size_t n)
{
    tester<int> p11995;

    while (n--)
    {
        int command, x;
        cin >> command >> x;

        switch (command)
        {
        case 1:
            p11995.push(x);
            break;
        case 2:
            p11995.pop(x);
            break;
        default:
            break;
        }
    }

    cout << p11995.result() << endl;
}

int main()
{
    size_t n;
    while (cin >> n) test_case(n);
    return 0;
}

以上是关于c_cpp uva11995的主要内容,如果未能解决你的问题,请参考以下文章

UVA 11995 (STL_E题)解题报告

Uva 11995 I Can Guess the Data Structure!

UVa 11995 猜猜数据结构

uva 11995 I Can Guess the Data Structure!

uva 11995 I Can Guess the Data Structure!

UVA11995 I Can Guess the Data Structure!