博客
关于我
S:List
阅读量:419 次
发布时间:2019-03-06

本文共 1540 字,大约阅读时间需要 5 分钟。

描述

写一个程序完成以下命令:

new id ——新建一个指定编号为id的序列(id<10000)
add id num——向编号为id的序列加入整数num
merge id1 id2——合并序列id1和id2中的数,并将id2清空
unique id——去掉序列id中重复的元素
out id ——从小到大输出编号为id的序列中的元素,以空格隔开

输入第一行一个数n,表示有多少个命令( n<=200000)。以后n行每行一个命令。输出按题目要求输出。样例输入

16new 1new 2add 1 1add 1 2add 1 3add 2 1add 2 2add 2 3add 2 4out 1out 2merge 1 2out 1out 2unique 1out 1

样例输出

1 2 3 1 2 3 41 1 2 2 3 3 41 2 3 4

 

Approach #1: 

#include
#include
#include
#include
using namespace std;list
& FindList(vector
>& l, int id) { int tmp = l.size(); if (tmp > 0) { vector
>::iterator i; i = l.begin(); return *(i+id-1); }};int main() { int n; cin >> n; vector
> a; for (int i = 0; i < n; ++i) { string s; cin >> s; if (s == "new") { int id; cin >> id; a.push_back(list
()); } else if (s == "add") { int id, num; cin >> id >> num; list
& temp = FindList(a, id); temp.push_back(num); temp.sort(); } else if (s == "merge") { int id1, id2; cin >> id1 >> id2; list
& temp1 = FindList(a, id1); list
& temp2 = FindList(a, id2); temp1.merge(temp2); } else if (s == "unique") { int id; cin >> id; list
& temp = FindList(a, id); temp.unique(); } else if (s == "out") { int id; cin >> id; list
& temp = FindList(a, id); temp.sort(); if (temp.size() > 0) { list
::iterator it; for (it = temp.begin(); it != temp.end(); ++it) { cout << *it << " "; } } cout << endl; } } return 0;}

  

Analysis:

自己刚开始想的使用map来做这道题,样例通过了,但是提交的时候还是WA。参考了一下别人的代码交了上去。

 

转载地址:http://kwtuz.baihongyu.com/

你可能感兴趣的文章
mysql8.0新特性-自增变量的持久化
查看>>
Mysql8.0注意url变更写法
查看>>
Mysql8.0的特性
查看>>
MySQL8修改密码报错ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
查看>>
MySQL8修改密码的方法
查看>>
Mysql8在Centos上安装后忘记root密码如何重新设置
查看>>
Mysql8在Windows上离线安装时忘记root密码
查看>>
MySQL8找不到my.ini配置文件以及报sql_mode=only_full_group_by解决方案
查看>>
mysql8的安装与卸载
查看>>
MySQL8,体验不一样的安装方式!
查看>>
MySQL: Host '127.0.0.1' is not allowed to connect to this MySQL server
查看>>
Mysql: 对换(替换)两条记录的同一个字段值
查看>>
mysql:Can‘t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock‘解决方法
查看>>
MYSQL:基础——3N范式的表结构设计
查看>>
MYSQL:基础——触发器
查看>>
Mysql:连接报错“closing inbound before receiving peer‘s close_notify”
查看>>
mysqlbinlog报错unknown variable ‘default-character-set=utf8mb4‘
查看>>
mysqldump 参数--lock-tables浅析
查看>>
mysqldump 导出中文乱码
查看>>
mysqldump 导出数据库中每张表的前n条
查看>>