Description Write a program to complete the following command: new id - create a specified sequence of Numbers for id (id < 200000) Add id num - to join the sequence of Numbers for id integer num merge id1 id2 - merger sequence number of id1 and id2, and make id2 empty unique id - remove repetitive element in the sequence id out id - from small to large, output the elements of sequence of Numbers for ids separated by Spaces Input The First Line is n which mean the number of the Command (n <= 200000). The next n lines are correct Command. You don't have to worry wrong input and id is int. Output Then output according the requirements. Sample Input 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 Sample Ouput 1 2 3 1 2 3 4 1 1 2 2 3 3 4 1 2 3 4 1 2 3 1 2 3 4 1 1 2 2 3 3 4 1 2 3 4
时间: 2024-01-05 09:02:58 浏览: 61
Mysql:The user specified as a definer ('xxx@'%') does not exist的解决方案
5星 · 资源好评率100%
以下是 Python 代码实现:
```python
n = int(input())
seqs = {}
for i in range(n):
cmd = input().split()
if cmd[0] == "new":
id = int(cmd[1])
seqs[id] = []
elif cmd[0] == "add":
id = int(cmd[1])
num = int(cmd[2])
seqs[id].append(num)
elif cmd[0] == "merge":
id1 = int(cmd[1])
id2 = int(cmd[2])
seqs[id1] += seqs[id2]
seqs[id2] = []
elif cmd[0] == "unique":
id = int(cmd[1])
seqs[id] = list(set(seqs[id]))
elif cmd[0] == "out":
id = int(cmd[1])
seqs[id].sort()
print(" ".join([str(num) for num in seqs[id]]))
```
首先读入命令数量 n,然后使用一个字典 seqs 来存储每个序列对应的元素序列。对于每个命令,根据命令的不同进行相应的操作。
对于 "new" 命令,直接在 seqs 中创建一个空列表来表示新的序列。
对于 "add" 命令,将指定元素添加到对应序列的末尾。
对于 "merge" 命令,将两个序列合并,并将右侧序列清空。
对于 "unique" 命令,将对应序列去重。
对于 "out" 命令,先将对应序列排序,然后输出序列中的元素,用空格隔开。
注意,在输出时需要将元素转换为字符串类型,否则会出现类型错误。
阅读全文