编程介的小学生 2017-02-17 06:56 采纳率: 20.5%
浏览 936
已采纳

Outernet

A company named Outdaters is running a small computer wire line network, called Outernet. Not like Internet, Outernet is not base on the TCP/IP protocol. Due to lacking of money, not all the computers in Outernet can communicate with each other directly.

Outdaters have already found the solution. They created a protocol to make all computers in the network become application proxies. An application proxy can receive data from a connected computer and send them out to another connected computer. Therefore, by using this protocol in Outernet, if a computer wants to send something to a computer not linked directly, it has to send them to a connected computer/application proxy and ask it to help sending them to the destination or another connected computer/application proxy.

The protocol is described as

A. Port

Application proxies use port to indicate each connected computer. Port number is an integer number from 0 to 32,767. To an application proxy, 0 means the application proxy itself, each other port number represents a unique computer connected to the application proxy.

B. Commands

The application proxy accepts only 3 commands, case sensitive: TO, DATA, QUIT.

To each incoming command, application proxy will response with 3-digit result code in a line to the incoming port after handled this command.

Format:
xxx xxx - the 3-digit result code

Result codes:

100: OK. No error/Data routed to destination
101: OK. Data routed to application. (Destination computer is application proxy itself.)

200: Session ends (Response to QUIT command)

300: Unknown command
301: Unknown destination
302: No session began
303: Looping not allowed (when incoming port = outgoing port)

Details for each command:

  1. TO:

Tell the application proxy, the following data need to be sent to , and cancel the last "TO" command's effect (send a "QUIT" command to the original destination computer). If TO command fails (result code is neither 100 nor 101), the state of the application proxy will not be changed.

Possible result codes are:
100: The destination computer is found in routing table, and not the application proxy itself.
101: The destination computer is found in routing table, and IS the application proxy itself.
301: The destination computer is not found in routing table
303: The destination computer is found in routing table, but incoming port = outgoing port

  1. DATA

Send to destination computer. will be regarded as a data stream, and sent to the destination without any alteration if the destination computer is not the application proxy itself. The backslash ("\") is the meta character, "." means a simple dot ".", instead of then end indicator, and "\" means "\".

Possible result codes are:
100: The destination computer is found in routing table, and not the application proxy itself. The data is routed to the corresponding outgoing port.
101: The destination computer is found in routing table, and IS the application proxy itself. The data is routed to the application running on this application proxy.
302: No session began, this command is ignored.

  1. QUIT

End this communication session.

Possible result codes are:
200: Session ends(Response to QUIT command)
302: No session began, this command is ignored.

C. Session

When a computer (the requester) sends a "TO" command to an application proxy, a communication session begins; when a "QUIT" command is sent to the application proxy, the session ends. In a session, the requester can send multiple "TO" and "DATA" commands to an application proxy to send out multiple messages.

An application proxy is able to handle sessions simultaneously from different ports.

D. Routing table

Each application proxy holds a routing table. It uses this table to find which port should be used the destination computer name. Each line in the routing table contains 2 fields, the first is the destination computer name, and the second is the outgoing port number. It means, the data to be sent to a computer with the destination computer name, will be sent out via the port with the outgoing port number. Port number 0 means, the data should be routed to the application running on this application proxy; that destination computer name is actually the application proxy's name.

E. Routing

Application proxies use the same "TO", "DATA", "QUIT" commands to route the incoming data if the routing is possible.

After searching on the routing table, if the outgoing port found, application proxies must create a complete session on the outgoing port for each valid incoming "TO" command: one "TO" command at the beginning, zero or more DATA commands to route the data, one "QUIT" command in the end if the incoming session ends or another incoming "TO" command is received.

Port 0 is handled as same as other outgoing ports except that no actually outgoing command is sent, i.e. all the commands�� result code will be sent to the incoming port, but no commands will be sent to any outgoing port.

Now, Outdaters hires you to write the engine to implement the protocol for the application proxy.

Input

The input consists of a sequence of testcases. Each begins with a routing table of an application proxy and then the incoming requests of the application proxy.

A routing table includes, in order, a line with an integer M (1 <= M <= 32,768), the number of lines in the routing table; M lines, each of which has a routing line. Each routing line contains a unique destination computer name (1 to 15 alphanumeric characters in the routing table), and then the outgoing port number (0 to 32,767 integer), separated by a space, and the computer names are case sensitive.

The incoming requests of the application proxy include several request sessions from the connected computers. A line starts with a number sign "#" and then an integer P (-1, 1 to 32,767), means the following input is from port P, P.

The input is terminated by a single zero

Output

For each testcase, print all the outputs of the ports sending out data, in the order of the corresponding input. For each port��s output, a line starts with a number sign "#" and then an integer P (-1, 1 to 32,767), means the following commands are output in port P, P=-1 means the output of the current testcase finishes. Following the ��#�� line is the commands output in this port till another ��#�� line. A "#" line is needed only when the port number need to be changed.

#
the commands output in this port
#
the commands output in this port
...
#-1

Sample Input

5
RED 0
YELLOW 1
GREEN 2
BLUE 3
WHITE 3
#1
TO:GREEN
DATA
HELLO
.
#4
TO:WHITE
#1
Quit
QUIT
#2
TO:GREEN
DATA
A JOKE to myself
.
QUIT
#3
TO:ORANGE
QUIT
#4
QUIT
#-1
0

Sample Output

#2
TO:GREEN
#1
100
#2
DATA
HELLO
.
#1
100
#3
TO:WHITE
#4
100
#1
300
#2
QUIT
#1
200
#2
303
302
302
#3
301
302
QUIT
#4
200
#-1

  • 写回答

2条回答 默认 最新

  • threenewbee 2017-02-21 15:17
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)