#P7911. [CSP-J 2021] 网络连接
[CSP-J 2021] 网络连接
Problem Description
The TCP/IP protocol is an important protocol in the field of network communication. Today, your task is to try to use this protocol to reproduce a simplified network connection scenario.
In this problem, computers are divided into two categories: servers (Server) and clients (Client). Servers are responsible for establishing connections, and clients are responsible for joining connections.
There are a total of computers that need to perform network connection operations, numbered from to . These machines will, in increasing order of their indices, each initiate an operation to establish a connection or join a connection.
Each machine must provide an address string when attempting to establish or join a connection. The address string provided by a server indicates the address at which it tries to establish a connection, and the address string provided by a client indicates the address it tries to join.
A valid address string must have the following properties:
- It must be in the format
a.b.c.d:e, where are all non-negative integers. - , and .
- None of may contain extra leading zeros.
Accordingly, an invalid address string may have one or more of the following issues:
- It is not a string in the format
a.b.c.d:e, for example, it contains more than.characters or more than:character. - One or more of the integers is out of the above range.
- One or more of the integers contains extra leading zeros.
For example, the address string 192.168.0.255:80 is valid, but 192.168.0.999:80, 192.168.00.1:10, 192.168.0.1:088, and 192:168:0:1.233 are all invalid.
If the address string provided by a server or a client during an operation is invalid, that operation will be ignored directly.
In this problem, we assume that any address string that satisfies the above rules can participate in normal connections. You do not need to consider the real meaning of each address string.
Due to network congestion and other reasons, two servers are not allowed to use the same address string. If this happens, the later server attempting to establish a connection will fail to establish the connection. Apart from this, any server that provides a valid address string can successfully establish a connection.
If a client that provides a valid address string, when attempting to join a connection, has the same address string as some previously existing server that has successfully established a connection, then the client can successfully join the connection, and we say it connects to that server. If no such server can be found, then this client is considered unable to successfully join a connection.
Note that although two different servers are not allowed to use the same address string, it is allowed for multiple clients to use the same address string, and it is also allowed for multiple clients to connect to the same server at the same time.
Your task is simple: given the type and address string of each computer, determine the connection status of each computer.
Input Format
The first line contains a positive integer .
The next lines each contain two strings , describing the type and address string of each computer in increasing order of their indices.
Here, is guaranteed to be one of the strings Server or Client, and is a non-empty string of length at most , consisting only of digits, the character ., and the character :.
The two strings on each line are separated by exactly one space, and there are no extra spaces at the end of the line.
Output Format
Output lines. The -th line contains a positive integer or a string indicating the connection status of the -th computer. Specifically:
If the -th computer is a server, then:
- If it provides a valid address string and successfully establishes the connection, output the string
OK. - If it provides a valid address string but fails to establish the connection because a previous server has the same address string, output the string
FAIL. - If its address string is not valid, output the string
ERR.
If the -th computer is a client, then:
- If it provides a valid address string and successfully joins the connection, output a positive integer indicating the index of the server it connects to.
- If it provides a valid address string but fails to join the connection, output the string
FAIL. - If its address string is not valid, output the string
ERR.
5
Server 192.168.1.1:8080
Server 192.168.1.1:8080
Client 192.168.1.1:8080
Client 192.168.1.1:80
Client 192.168.1.1:99999
OK
FAIL
1
FAIL
ERR
10
Server 192.168.1.1:80
Client 192.168.1.1:80
Client 192.168.1.1:8080
Server 192.168.1.1:80
Server 192.168.1.1:8080
Server 192.168.1.999:0
Client 192.168.1.1.8080
Client 192.168.1.1:8080
Client 192.168.1.1:80
Client 192.168.1.999:0
OK
1
FAIL
FAIL
OK
ERR
ERR
5
1
ERR
见附件中的 network/network3.in。
见附件中的 network/network3.ans。
见附件中的 network/network4.in。
见附件中的 network/network4.ans。
Hint
[Sample Explanation #1]
Computer is a server and provides a valid address string 192.168.1.1:8080, so it successfully establishes the connection.
Computer is a server and provides the same address string as computer , so it fails to establish the connection.
Computer is a client and provides a valid address string 192.168.1.1:8080, so it successfully joins the connection and connects to server .
Computer is a client and provides a valid address string 192.168.1.1:80, but it cannot find a server to connect to.
Computer is a client, and its address string 192.168.1.1:99999 is invalid.
[Constraints]
| Test Point ID | Special Properties | |
|---|---|---|
| Properties 1 2 3 | ||
| Properties 1 2 | ||
| Property 1 | ||
| Property 2 | ||
| Property 4 | ||
| Property 5 | ||
| No special properties |
“Property 1”: It is guaranteed that all address strings are valid.
“Property 2”: It is guaranteed that for any two different computers, if they are both servers or both clients, then their address strings must be different.
“Property 3”: It is guaranteed that the index of any server is smaller than the indices of all clients.
“Property 4”: It is guaranteed that all address strings are in the format a.b.c.d:e, where are all non-negative integers not exceeding and without extra leading zeros.
“Property 5”: It is guaranteed that all address strings are in the format a.b.c.d:e, where are all non-empty strings consisting only of digits.
For of the data, it is guaranteed that .
[Thanks for providing hack testdata]
Translated by ChatGPT 5