Temp

P7911 [Guess where does it come from] Network Connection

Problem Description

The TCP/IP protocol is an important standard in network communication. Today, your task is to simulate a simplified version of a network connection scenario using this protocol.

In this problem, computers are divided into two types: Servers and Clients. 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 connections, numbered from 1 to n. These machines perform their actions (either establishing or joining a connection) in increasing order of their IDs.

Each computer must provide an address string when trying to establish or join a connection. A server provides the address it wants to bind to, and a client provides the address it wants to connect to.

A valid address string must meet the following criteria:

  1. It must be in the format a.b.c.d:e, where a, b, c, d, e are 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 have unnecessary leading zeros.

An invalid address string may violate the above rules, including but not limited to:

  • Not conforming to the a.b.c.d:e format, e.g., more than three . characters or more than one : character;
  • Any of the five integers a, b, c, d, e fall outside the specified ranges;
  • Any of the integers have leading zeros (unless the number is exactly 0).

For example:

  • 192.168.0.255:80 is valid;
  • 192.168.0.999:80, 192.168.00.1:10, 192.168.0.1:088, 192:168:0:1.233 are all invalid.

If a server or client provides an invalid address string, its operation is ignored.

Any valid address can be used for connections, and you don't need to worry about the actual meaning of addresses.

Important rules:

  • No two servers may bind to the same valid address. If a server tries to use an address already used by another server, it fails to establish the connection.
  • Clients can share addresses, and multiple clients can connect to the same server.
  • A client successfully connects only if it provides a valid address that matches an existing, successfully established server’s address.

Input Format

The first line contains a positive integer n.

Each of the next n lines contains two strings \texttt{op} and \texttt{ad}, denoting the type and address string of the i-th computer (in order).
- op is either Server or Client;
- ad is the address string provided by that computer.

Created on: June 3, 2025, 9:18 p.m.
Modified on: July 13, 2025, 10:03 p.m.
Views: 53