
正文
S:List
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
描述
写一个程序完成以下命令:
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行每行一个命令。输出按题目要求输出。样例输入
16
new 1
new 2
add 1 1
add 1 2
add 1 3
add 2 1
add 2 2
add 2 3
add 2 4
out 1
out 2
merge 1 2
out 1
out 2
unique 1
out 1
样例输出
1 2 3
1 2 3 4
1 1 2 2 3 3 4 1 2 3 4
Approach #1:
#include<iostream>
#include<iterator>
#include<list>
#include<vector>
using namespace std;
list<int>& FindList(vector<list<int>>& l, int id) {
int tmp = l.size();
if (tmp > 0) {
vector<list<int>>::iterator i;
i = l.begin();
return *(i+id-1);
}
}; int main() {
int n;
cin >> n;
vector<list<int>> a;
for (int i = 0; i < n; ++i) {
string s;
cin >> s;
if (s == "new") {
int id;
cin >> id;
a.push_back(list<int>());
} else if (s == "add") {
int id, num;
cin >> id >> num;
list<int>& temp = FindList(a, id);
temp.push_back(num);
temp.sort();
} else if (s == "merge") {
int id1, id2;
cin >> id1 >> id2;
list<int>& temp1 = FindList(a, id1);
list<int>& temp2 = FindList(a, id2);
temp1.merge(temp2);
} else if (s == "unique") {
int id;
cin >> id;
list<int>& temp = FindList(a, id);
temp.unique();
} else if (s == "out") {
int id;
cin >> id;
list<int>& temp = FindList(a, id);
temp.sort();
if (temp.size() > 0) {
list<int>::iterator it;
for (it = temp.begin(); it != temp.end(); ++it) {
cout << *it << " ";
}
}
cout << endl;
}
}
return 0;
}
Analysis:
自己刚开始想的使用map来做这道题,样例通过了,但是提交的时候还是WA。参考了一下别人的代码交了上去。
随机推荐
-
操作SQL Server的帮助类
可作为以后开发的参考代码,也可以再整理下,代码如下: using System; using System.Collections.Generic; using System.Linq; using ...
-
maven如何引入servlet-api和jsp-api
废话不多说,直接上代码 <dependency> <groupId>javax.servlet</groupId> <artifactId>javax. ...
-
linux进阶与hadoop
Linux进阶命令: find . | ls --help | more grep ll | grep 1.txt grep -ri BASH 1.txt grep -ri BASH ...
-
node-mysql中防止SQL注入
备注: 本文针对mysqljs/mysql. 为了防止SQL注入,可以将SQL中传入参数进行编码,而不是直接进行字符串拼接.在node-mysql中,防止SQL注入的常用方法有以下四种: 方法一:使用 ...
-
监控服务器cpu、磁盘、模板以及自定义key
一.检测主机存活 net.tcp.service.perf[tcp,,] Float型 返回0代表端口挂了 zabbix fping要开启sudo权限之类比较不方便 二.监控CPU负载 监控load ...
-
spring+springmvc+mybatis+redis 实现两重数据缓存
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http:// ...
-
[C++] Returning values by reference in C++
A C++ program can be made easier to read and maintain by using references rather than pointers. A C+ ...
-
p2408 不同子串个数
传送门 分析 首先我们不难求出一共有多少子串 之后我们只需要减掉重复个数即可 于是我们对于每个后缀减去它跟它前一名的最长公共前缀即可 代码 #include<iostream> #incl ...
-
JS中立即执行函数的理解
1.匿名函数不能单独定义,必须进行赋值操作或者立即执行,否则会被JS引擎定义为语法错误 function(){alert(dada);} VM229:1 Uncaught SyntaxError: U ...
-
Union、Union All、Intersect、Minus
转自:http://www.2cto.com/database/201208/148795.html Union:对两个结果集进行并集操作,不包括重复行,同时进行默认规则的排序: Union All: ...
可作为以后开发的参考代码,也可以再整理下,代码如下: using System; using System.Collections.Generic; using System.Linq; using ...
废话不多说,直接上代码 <dependency> <groupId>javax.servlet</groupId> <artifactId>javax. ...
Linux进阶命令: find . | ls --help | more grep ll | grep 1.txt grep -ri BASH 1.txt grep -ri BASH ...
备注: 本文针对mysqljs/mysql. 为了防止SQL注入,可以将SQL中传入参数进行编码,而不是直接进行字符串拼接.在node-mysql中,防止SQL注入的常用方法有以下四种: 方法一:使用 ...
一.检测主机存活 net.tcp.service.perf[tcp,,] Float型 返回0代表端口挂了 zabbix fping要开启sudo权限之类比较不方便 二.监控CPU负载 监控load ...
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http:// ...
A C++ program can be made easier to read and maintain by using references rather than pointers. A C+ ...
传送门 分析 首先我们不难求出一共有多少子串 之后我们只需要减掉重复个数即可 于是我们对于每个后缀减去它跟它前一名的最长公共前缀即可 代码 #include<iostream> #incl ...
1.匿名函数不能单独定义,必须进行赋值操作或者立即执行,否则会被JS引擎定义为语法错误 function(){alert(dada);} VM229:1 Uncaught SyntaxError: U ...
转自:http://www.2cto.com/database/201208/148795.html Union:对两个结果集进行并集操作,不包括重复行,同时进行默认规则的排序: Union All: ...







