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 4100
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上跑通,但在求交集的算法仍然存在问题。
1218. settest相关推荐
- 信息学奥赛一本通(1218:取石子游戏)
1218:取石子游戏 时间限制: 1000 ms 内存限制: 65536 KB 提交数: 8837 通过数: 4144 [题目描述] 有两堆石子,两个人轮流去取.每次取的时候, ...
- 1218 图片对齐模式
1218 图片对齐模式 行线 图片对齐模式 作用于img标签上
- 1218 鼠标样式 cursor
1218 鼠标样式 cursor 示例代码 .p1{cursor: default;}.p2{cursor: pointer;}.p3{cursor: move;}.p4{cursor: text;} ...
- 1218 溢出设置 overflow
1218 溢出设置 overflow overflow属性 元素益出设置 overflow值 visible,可见的 hidden,隐藏 scroll,滚动条 auto,自动显示滚动条
- 熟悉HTML基本标签的分类测试分析 1218
熟悉HTML基本标签的分类测试分析 1218 01 02
- ktv 上传图片 1218
ktv 上传图片 1218 打开会话框. 效果 文件图片OK事件 文件的路径与文件的名称 图片盒子图片指定 给一个图片的绝对路径.... 验证图片的格式
- uestc oj 1218 Pick The Sticks (01背包变形)
题目链接:http://acm.uestc.edu.cn/#/problem/show/1218 给出n根木棒的长度和价值,最多可以装在一个长 l 的容器中,相邻木棒之间不允许重叠,且两边上的木棒,可 ...
- 1218: 青蛙(三)
1218: 青蛙(三) 1.描述 青蛙一族的长老蛤蟆文太也知道一只青蛙一张嘴两只眼睛四条腿,但青蛙就是青蛙,他想请你帮他算出来有多少只青蛙,你敢不算么?小心蛤蟆文太揍你-- 蛤蟆文太在此. 输入 多组 ...
- NBUT 1218 You are my brother
[1218] You are my brother 时间限制: 1000 ms 内存限制: 131072 K 问题描述 Little A gets to know a new friend, Litt ...
最新文章
- 多线激光雷达~三维建图
- springboot 做表白墙_学校表白墙有多羞耻??!辣眼分析数千条表白内容,原来脱单秘密在这里.......
- Java中常见的几种类型转换
- 公司承担的国家发改委高技术产业化示范工程
- zoj 1366 Cash Machine
- 0603学术诚信与职业道德
- Ubuntu安装Docker引擎和支持HTTPS的docker-registry服务
- 基于JAVA+SpringMVC+Mybatis+MYSQL的音乐播放系统
- 2016/05/13 thinkphp 3.2.2 ① 数据删除及执行原生sql语句 ②表单验证
- VM中的Linux安装jdk和tomcat
- 分三种情况C语言编程,吴进的256basic.h阅读笔记,请问scanline_copy子程序为什么要分三种情况考虑:(1)d...
- 华为数通笔记-数通基本概念
- php 会议室预定系统,MRBS开源会议室预订系统安装
- origin画已知函数曲线_使用Origin进行函数绘图的方法
- [江枫]In Memory Undo与logical standby database
- 计算机对电影工业的影响的英语作文,看电影的好处The Advantages of Watching Movies
- SAP物料凭证中的凭证类型交易/事件
- 分享6款优秀的 AR/VR 开源库 – 切切歆语的博客 – CSDN博客
- 当当网高可用架构之道
- OpenCV-实现天空变换(图像分割)
热门文章
- 如何打开WPS文档的目录?
- Pygame实战:Python开挂版无敌小恐龙【源码免费领】
- 线程和进程/阻塞和挂起以及那些sleep,wait()和notify()方法详解
- 利用Python制作旋转花灯,祝大家元宵节快乐
- 网站十种常见盈利模式简介
- 移动机器人里程计校准的方法
- eclipse+tomcat debug不用重启方法
- 基础编程题目集 ——7-19 支票面额
- 初三计算机模拟考试在房考吗,中考临近,初三学生想知道中考难还是模拟考试难,你知道哪个难吗...
- 多线程爬取zxcs.me【代码记录】