
题目描述某网络设备采用收发机制管理数据包该机制包含两个区域缓冲区和发送区并支持以下指令1.RECEIVE x接收到编号为x的数据包。具体规则如下如果缓冲区没有该编号的数据包则接收成功将其存入缓冲区输出数据包编号- 如果缓冲区存在该编号的数据包则接收失败输出12. SEND发送一个数据包。具体规则如下- 如果发送区不为空则从发送区取出一个最早的数据包发送输出数据包编号如果发送区为空但缓冲区不为空则将缓冲区中的所有数据包转移到发送区然后取出最早接收的数据包进行发送输出数据包编号- 如果两个区域都为空则输出03.QUERY查询最早接收但尚未发送的数据包编号。具体规则如下如果发送区不为空则输出发送区中最早的数据包编号如果发送区为空但缓冲区不为空则将缓冲区中的所有数据包转移到发送区然后输出最早接收的数据包编号- 如果两个区域都为空则输出0输入格式接收到的指令集。输出格式对指令处理输出的结果集。补充说明数据范围指令集数量n: 1≤n ≤100数据包编号x:1≤x≤1000输入的所有指令格式保证合法示例1输入[RECEIVE 1,RECEIVE 2,QUERY,SEND,QUERY.SEND]输出[1.2.1.1.2.2]说明RECEIVE 1缓冲区无重复数据包接收成功输出1RECEIVE 2: 缓冲区无重复数据包接收成功输出2QUERY发送区为空将缓冲区数据移至发送区最早接收1输出1SEND发送区1,2发送并输出1QUERY发送区剩余2输出2SEND发送区剩余2发送并输出2示例2输入[RECEIVE 3,RECEIVE 3,SEND,SEND,SEND]输出[3.-1.3.0.0]说明RECEIVE 3缓冲区无重复数据包接收成功输出3RECEIVE 3缓冲区存在重复数据包接收失败输出1SEND发送区为空将缓冲区数据移至发送区发送并输出3SEND发送区和缓冲区均为空输出0SEND发送区和缓冲区均为空输出0示例3输入[RECEIVE 1]输出[1]说明RECEIVE 1缓冲区无重复数据包接收成功输出1问题分析与解答思路题目要求模拟网络设备的收发机制处理指令序列并输出结果。根据指令描述需要考虑是否重复接收、发送顺序及查询操作。使用队列数据结构维护缓冲区区和发送区配合集合进行高效查重。解答思路初始化缓冲区队列、发送区队列和缓冲区集合。解析每条指令RECEIVE x检查是否重复使用集合若无则加入缓冲区队列和集合输出x否则输出-1。SEND若发送区非空则发送最早元素并输出否则移动缓冲区数据到发送区保序再发送最早元素若均空则输出0。QUERY类似SEND但不实际移除数据直接返回最早元素或转移后查询。注意移动缓冲区数据时需清空缓冲区并维护顺序。输出数组与指令一一对应。C 实现#include vector #include string #include queue #include unordered_set #include sstream std::vectorint processInstructions(const std::vectorstd::string instructions) { std::queueint bufQueue; std::queueint sendQueue; std::unordered_setint bufSet; std::vectorint output; for (const auto inst : instructions) { if (inst.find(RECEIVE ) 0) { int x std::stoi(inst.substr(8)); if (bufSet.find(x) ! bufSet.end()) { output.push_back(-1); } else { bufQueue.push(x); bufSet.insert(x); output.push_back(x); } } else if (inst SEND) { if (!sendQueue.empty()) { output.push_back(sendQueue.front()); sendQueue.pop(); } else if (!bufQueue.empty()) { while (!bufQueue.empty()) { sendQueue.push(bufQueue.front()); bufQueue.pop(); } bufSet.clear(); output.push_back(sendQueue.front()); sendQueue.pop(); } else { output.push_back(0); } } else if (inst QUERY) { if (!sendQueue.empty()) { output.push_back(sendQueue.front()); } else if (!bufQueue.empty()) { while (!bufQueue.empty()) { sendQueue.push(bufQueue.front()); bufQueue.pop(); } bufSet.clear(); output.push_back(sendQueue.front()); } else { output.push_back(0); } } } return output; }Java 实现import java.util.*; public class PacketProcessor { public ListInteger processInstructions(ListString instructions) { QueueInteger bufQueue new LinkedList(); QueueInteger sendQueue new LinkedList(); SetInteger bufSet new HashSet(); ListInteger output new ArrayList(); for (String inst : instructions) { if (inst.startsWith(RECEIVE )) { int x Integer.parseInt(inst.substring(8).trim()); if (bufSet.contains(x)) { output.add(-1); } else { bufQueue.add(x); bufSet.add(x); output.add(x); } } else if (inst.equals(SEND)) { if (!sendQueue.isEmpty()) { output.add(sendQueue.poll()); } else if (!bufQueue.isEmpty()) { while (!bufQueue.isEmpty()) { sendQueue.add(bufQueue.poll()); } bufSet.clear(); output.add(sendQueue.poll()); } else { output.add(0); } } else if (inst.equals(QUERY)) { if (!sendQueue.isEmpty()) { output.add(sendQueue.peek()); } else if (!bufQueue.isEmpty()) { while (!bufQueue.isEmpty()) { sendQueue.add(bufQueue.poll()); } bufSet.clear(); output.add(sendQueue.peek()); } else { output.add(0); } } } return output; } }JavaScript 实现function processInstructions(instructions) { let bufQueue []; let sendQueue []; let bufSet new Set(); let output []; for (let inst of instructions) { if (inst.startsWith(RECEIVE )) { let x parseInt(inst.substring(8).trim()); if (bufSet.has(x)) { output.push(-1); } else { bufQueue.push(x); bufSet.add(x); output.push(x); } } else if (inst SEND) { if (sendQueue.length 0) { output.push(sendQueue.shift()); } else if (bufQueue.length 0) { while (bufQueue.length 0) { sendQueue.push(bufQueue.shift()); } bufSet.clear(); output.push(sendQueue.shift()); } else { output.push(0); } } else if (inst QUERY) { if (sendQueue.length 0) { output.push(sendQueue[0]); } else if (bufQueue.length 0) { while (bufQueue.length 0) { sendQueue.push(bufQueue.shift()); } bufSet.clear(); output.push(sendQueue[0]); } else { output.push(0); } } } return output; }Go 实现package main import ( strconv strings ) func processInstructions(instructions []string) []int { bufQueue : []int{} sendQueue : []int{} bufSet : make(map[int]bool) output : []int{} for _, inst : range instructions { if strings.HasPrefix(inst, RECEIVE ) { x, _ : strconv.Atoi(strings.TrimSpace(inst[8:])) if _, exists : bufSet[x]; exists { output append(output, -1) } else { bufQueue append(bufQueue, x) bufSet[x] true output append(output, x) } } else if inst SEND { if len(sendQueue) 0 { output append(output, sendQueue[0]) sendQueue sendQueue[1:] } else if len(bufQueue) 0 { for _, pkt : range bufQueue { sendQueue append(sendQueue, pkt) } bufQueue []int{} bufSet make(map[int]bool) output append(output, sendQueue[0]) sendQueue sendQueue[1:] } else { output append(output, 0) } } else if inst QUERY { if len(sendQueue) 0 { output append(output, sendQueue[0]) } else if len(bufQueue) 0 { for _, pkt : range bufQueue { sendQueue append(sendQueue, pkt) } bufQueue []int{} bufSet make(map[int]bool) output append(output, sendQueue[0]) } else { output append(output, 0) } } } return output }Python 实现import collections def process_instructions(instructions): buf_queue collections.deque() send_queue collections.deque() buf_set set() output [] for inst in instructions: if inst.startswith(RECEIVE ): x int(inst.split()[1]) if x in buf_set: output.append(-1) else: buf_queue.append(x) buf_set.add(x) output.append(x) elif inst SEND: if send_queue: output.append(send_queue.popleft()) elif buf_queue: while buf_queue: send_queue.append(buf_queue.popleft()) buf_set.clear() output.append(send_queue.popleft()) else: output.append(0) elif inst QUERY: if send_queue: output.append(send_queue[0]) elif buf_queue: while buf_queue: send_queue.append(buf_queue.popleft()) buf_set.clear() output.append(send_queue[0]) else: output.append(0) return output验证说明以上代码均按照题目要求实现处理指令序列并输出正确结果。举例验证示例1输入[RECEIVE 1,RECEIVE 2,QUERY,SEND,QUERY,SEND]输出[1, 2, 1, 1, 2, 2]符合题意。示例2输入[RECEIVE 3,RECEIVE 3,SEND,SEND,SEND]输出[3, -1, 3, 0, 0]正确处理重复包和空发送。示例3输入[RECEIVE 1]输出[1]单指令成功处理。使用队列保证操作高效性从复杂度分析满足题目规模要求集合用于快速查重。