P7911 [CSP-J 2021] Network Connection
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 $n$ computers that need to perform network connection operations, numbered from $1$ to $n$. 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:
1. It must be in the format `a.b.c.d:e`, where $a, b, c, d, e$ are all non-negative integers.
2. $0 \le a, b, c, d \le 255$, and $0 \le e \le 65535$.
3. None of $a, b, c, d, e$ may contain extra leading zeros.
Accordingly, an invalid address string may have one or more of the following issues:
1. It is not a string in the format `a.b.c.d:e`, for example, it contains more than $3$ `.` characters or more than $1$ `:` character.
2. One or more of the integers $a, b, c, d, e$ is out of the above range.
3. One or more of the integers $a, b, c, d, e$ 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 $n$.
The next $n$ lines each contain two strings $\mathit{op}, \mathit{ad}$, describing the type and address string of each computer in increasing order of their indices.
Here, $\mathit{op}$ is guaranteed to be one of the strings `Server` or `Client`, and $\mathit{ad}$ is a non-empty string of length at most $25$, 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 $n$ lines. The $i$-th line contains a positive integer or a string indicating the connection status of the $i$-th computer. Specifically:
If the $i$-th computer is a server, then:
1. If it provides a valid address string and successfully establishes the connection, output the string `OK`.
2. 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`.
3. If its address string is not valid, output the string `ERR`.
If the $i$-th computer is a client, then:
1. 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.
2. If it provides a valid address string but fails to join the connection, output the string `FAIL`.
3. If its address string is not valid, output the string `ERR`.
Explanation/Hint
**[Sample Explanation #1]**
Computer $1$ is a server and provides a valid address string `192.168.1.1:8080`, so it successfully establishes the connection.
Computer $2$ is a server and provides the same address string as computer $1$, so it fails to establish the connection.
Computer $3$ is a client and provides a valid address string `192.168.1.1:8080`, so it successfully joins the connection and connects to server $1$.
Computer $4$ is a client and provides a valid address string `192.168.1.1:80`, but it cannot find a server to connect to.
Computer $5$ is a client, and its address string `192.168.1.1:99999` is invalid.
**[Constraints]**
| Test Point ID | $n \le$ | Special Properties |
|:-:|:-:|:-:|
| $1$ | $10$ | Properties 1 2 3 |
| $2 \sim 3$ | $100$ | Properties 1 2 3 |
| $4 \sim 5$ | $1000$ | Properties 1 2 3 |
| $6 \sim 8$ | $1000$ | Properties 1 2 |
| $9 \sim 11$ | $1000$ | Property 1 |
| $12 \sim 13$ | $1000$ | Property 2 |
| $14 \sim 15$ | $1000$ | Property 4 |
| $16 \sim 17$ | $1000$ | Property 5 |
| $18 \sim 20$ | $1000$ | 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 $a, b, c, d, e$ are all non-negative integers not exceeding ${10}^9$ and without extra leading zeros.
“Property 5”: It is guaranteed that all address strings are in the format `a.b.c.d:e`, where $a, b, c, d, e$ are all non-empty strings consisting only of digits.
For $100 \%$ of the data, it is guaranteed that $1 \le n \le 1000$.
**[Thanks for providing hack testdata]**
- [xyf007](/user/68273).
Translated by ChatGPT 5