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.
时间: 2024-02-14 13:13:40 浏览: 53
Sure, I can help you with that! Here's an implementation in Python:
```
n = int(input())
sequences = {}
for i in range(n):
command = input().split()
if command[0] == 'new':
id = int(command[1])
sequences[id] = []
elif command[0] == 'add':
id = int(command[1])
num = int(command[2])
sequences[id].append(num)
elif command[0] == 'merge':
id1 = int(command[1])
id2 = int(command[2])
sequences[id2] += sequences[id1]
sequences[id1] = []
elif command[0] == 'unique':
id = int(command[1])
sequences[id] = list(set(sequences[id]))
elif command[0] == 'out':
id = int(command[1])
sequence = sorted(sequences[id])
print(' '.join(str(x) for x in sequence))
```
This program reads in the number of commands `n`, and then loops through each of the `n` commands. It uses a dictionary called `sequences` to keep track of the sequences of Numbers for each id.
For each command, the program checks the first word to determine what action to take. If the first word is `new`, it creates a new empty sequence for the specified id. If the first word is `add`, it adds the specified number to the sequence for the specified id. If the first word is `merge`, it merges the sequence for the first id into the sequence for the second id, and clears the first sequence. If the first word is `unique`, it removes duplicate elements from the sequence for the specified id. If the first word is `out`, it sorts the sequence for the specified id and prints it out as a space-separated list.
I hope this helps! Let me know if you have any questions or if there's anything else I can do for you.
阅读全文