1218. settest

Description

初始有一空集,然后对这个集合共有 n 次操作,每次给定一个集合以及操作名称(并: + / 差: - / 交: *)。每个操作完成后输出当前集合所有元素(均为整数,从小到大输出)。

Input Format

第 1 行: 一个数, n

第 2…(n + 1) 行: 每行首先一个字符 c (+/-/*),表示当前操作名称.

接下来一个整数 m,表示给定的集合元素个数.

接下来 m 个整数,给定这个集合里所有元素。

Output Format

输出共 n 行: 每行按照元素从小到大的顺序输出在执行完当前操作后你手上集合的所有元素。

Sample Input:

7
+ 3 1 2 3
- 1 2
+ 3 4 5 6
* 2 1 4
- 2 1 4
+ 1 100
- 1 99

Sample Output:

1 2 3
1 3
1 3 4 5 6
1 4

100
100

Limits

n, m <= 100

用自行编写的栈stack和二叉查找树实现如下:

#include <iostream>

using namespace std;

template<class elemType>
class stack {
public:
    virtual bool isEmpty() const = 0;

    virtual void push(const elemType &x) = 0;

    virtual elemType pop() = 0;

    virtual elemType top() const = 0;

    virtual ~stack() {};
};

template<class elemType>
class linkStack : public stack<elemType> {
private:
    struct node {
        elemType data;
        node *next;

        node(const elemType &x, node *N = NULL) {
            data = x;
            next = N;
        }

        node() : next(NULL) {}

        ~node() {}
    };

    node *top_p;
public:
    linkStack() {
        top_p = NULL;
    }

    ~linkStack() {
        node *tmp;
        while (top_p != NULL) {
            tmp = top_p;
            top_p = top_p->next;
            delete tmp;
        }
    }

    bool isEmpty() const {
        return top_p == NULL;
    }

    void push(const elemType &x) {
        top_p = new node(x, top_p);
    }

    elemType pop() {
        node *tmp = top_p;
        elemType x = tmp->data;
        top_p = top_p->next;
        delete tmp;
        return x;
    }

    elemType top() const {
        return top_p->data;
    }
};

template<class Type>
class BinarySearchTree {
private:
    struct BinaryNode {
        Type data;
        BinaryNode *left;
        BinaryNode *right;

        BinaryNode(const Type &thedata, BinaryNode *lt, BinaryNode *rt) : data(thedata), left(lt), right(rt) {}
    };

    struct StNode {
        BinaryNode *node;
        int TimesPop;

        StNode(BinaryNode *N = NULL) : node(N), TimesPop(0) {}
    };

    BinaryNode *root;

    void insert(const Type &x, BinaryNode *&t);

    void remove(const Type &x, BinaryNode *&t);

    bool find(const Type &x, BinaryNode *t) const;

    void clear(BinaryNode *&t);

public:

    BinarySearchTree(BinaryNode *t = NULL) { root = t; }

    ~BinarySearchTree() { makeEmpty(root); }

    bool find(const Type &x) const { return find(x, root); }

    void insert(const Type &x) { insert(x, root); }

    void remove(const Type &x) { remove(x, root); }

    void midOrder() const;

    void clear() {
        clear(root);
    }

    bool isEmpty() const {
        return root == NULL;
    }
};

template<class T>
void BinarySearchTree<T>::clear(BinarySearchTree<T>::BinaryNode *&t) {
    if (t == NULL) return;
    clear(t->left);
    clear(t->right);
    delete t;
    t = NULL;
}

template<class Type>
void BinarySearchTree<Type>::midOrder() const {
    if (root == NULL)
        return;
    linkStack<StNode> s;
    StNode current(root);
    s.push(current);
    while (!s.isEmpty()) {
        current = s.top();
        s.pop();
        if (++current.TimesPop == 2) {
            cout << current.node->data << " ";
            if (current.node->right != NULL)
                s.push(StNode(current.node->right));
        } else {
            s.push(current);
            if (current.node->left != NULL)
                s.push(StNode(current.node->left));
        }
    }
}

template<class Type>
bool BinarySearchTree<Type>::find(const Type &x, BinarySearchTree<Type>::BinaryNode *t) const {
    if (t == NULL)return false;
    else if (x < t->data)return find(x, t->left);
    else if (t->data < x)return find(x, t->right);
    else return true;
}

template<class Type>
void BinarySearchTree<Type>::insert(const Type &x, BinarySearchTree<Type>::BinaryNode *&t) {
    if (t == NULL)t = new BinaryNode(x, NULL, NULL);
    else if (x <= t->data)insert(x, t->left);
    else if (t->data < x)insert(x, t->right);
}

template<class Type>
void BinarySearchTree<Type>::remove(const Type &x, BinarySearchTree<Type>::BinaryNode *&t) {
    if (t == NULL)return;
    if (x < t->data)remove(x, t->left);
    else if (t->data < x)remove(x, t->right);
    else if (t->left != NULL && t->right != NULL) {
        BinaryNode *tmp = t->right;
        while (tmp->left != NULL)tmp = tmp->left;
        t->data = tmp->data;
        remove(t->data, t->right);
    } else {
        BinaryNode *oldNode = t;
        t = (t->left != NULL) ? t->left : t->right;
        delete oldNode;
    }
}

int main() {
    BinarySearchTree<int> *tree = new BinarySearchTree<int>();
    int numOfOperation;
    cin >> numOfOperation;
    for (int j = 0; j < numOfOperation; ++j) {
        char a;
        int b;
        cin >> a >> b;
        if (a == '+') {
            for (int i = 0; i < b; ++i) {
                int num;
                cin >> num;
                if (!tree->find(num)) {
                    tree->insert(num);
                }
            }
            if (!tree->isEmpty()) {
                tree->midOrder();
            }
            cout << endl;
        } else if (a == '-') {
            for (int i = 0; i < b; ++i) {
                int num;
                cin >> num;
                if (tree->find(num)) {
                    tree->remove(num);
                }
            }
            if (!tree->isEmpty()) {
                tree->midOrder();
            }
            cout << endl;
        } else {
            int num[b];
            for (int i = 0; i < b; ++i) {
                cin >> num[i];
                if (!tree->find(num[i])) {
                    num[i] = -10000;
                }
            }
            tree->clear();
            for (int i = 0; i < b; ++i) {
                if (num[i] != -10000) {
                    tree->insert(num[i]);
                }
            }
            if (!tree->isEmpty()) {
                tree->midOrder();
            }
            cout << endl;
        }
    }
    return 0;
}

注意:这里只是能在OJ上跑通,但在求交集的算法仍然存在问题。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值