#P7911. TCP/IP Simplified Network Connection
TCP/IP Simplified Network Connection
TCP/IP Simplified Network Connection
This problem involves simulating a simplified network connection scenario that uses a variant of the TCP/IP protocol. In this simplified model, there are two types of computers: Server
and Client
. A Server establishes a connection by providing an address string, whereas a Client tries to join an existing connection by providing an address string as well.
There are n computers, numbered from 1 to n. They attempt to perform their operations one after another in increasing order of their numbers. Each operation consists of the computer's type and an address string.
An address string is considered valid if and only if it meets all of the following criteria:
- The format must be exactly a.b.c.d:e, where a, b, c, d, e are non-negative integers.
- For the IP part, each number a, b, c, d must satisfy \(0 \le a,b,c,d \le 255\). For the port e, \(0 \le e \le 65535\).
- None of the numbers a, b, c, d, e should contain extra leading zeros (i.e. "0" is allowed, but "00", "01" etc. are not).
Examples: The address string 192.168.0.255:80
is valid, whereas 192.168.0.999:80
, 192.168.00.1:10
, 192.168.0.1:088
, and 192:168:0:1.233
are invalid.
When a computer provides an invalid address string, its operation is ignored. For a valid address string:
- If a Server uses a valid address string, it will successfully establish a connection if and only if no previous Server has already successfully established a connection using the same address string. If a duplicate occurs, the later server fails to connect.
- If a Client provides a valid address string and there is at least one previously successful Server that used the same address string, then the client successfully connects to that server (choose the earliest such server). Otherwise, the client fails to connect.
Your task is simple: Given the computer type and its address string for each computer, determine the connection outcome for each operation.
Output Format: For each computer in input order, print a single line:
- If the computer is a Server: Output "Server OK" if it successfully establishes a connection; otherwise, output "Server FAIL".
- If the computer is a Client: If it successfully connects, output "Client X" where X is the number of the server to which it connected; otherwise, output "Client FAIL".
inputFormat
The first line contains an integer n (\(1 \le n \le 10^5\)), the number of computers.
Each of the following n lines contains a computer operation in the format:
Type AddressString
Here, Type
is either "Server" or "Client", and AddressString
is a string.
outputFormat
For each of the n computers (in the order given), output one line indicating the connection result.
- If the computer is a Server: output "Server OK" if the connection is successfully established; otherwise, output "Server FAIL".
- If it is a Client: if it connects to a server (i.e. there exists a previously connected server using exactly the same valid address string), output "Client X" where X is the number (ID) of that server; otherwise, output "Client FAIL".
sample
5
Server 192.168.0.1:80
Client 192.168.0.1:80
Server 192.168.0.1:80
Server 10.0.0.1:8080
Client 10.0.0.1:8080
Server OK
Client 1
Server FAIL
Server OK
Client 4
</p>