#P9271. [CEOI 2013] 停车场 / Splot

[CEOI 2013] 停车场 / Splot

Background

Translated from CEOI 2013 Day 1

Along the Adriatic coast and its islands, there are beautiful beaches of many shapes and sizes. However, many beaches cannot be reached by car. To meet the growing demand, a huge open area near the coast was converted into a parking lot。

Since all architects involved in the design had an electrical engineering background and wanted to apply what they had learned, the layout of the parking lot ended up resembling the series-parallel diagrams commonly used in circuit design。

The parking lot consists of parking spaces and bidirectional roads connecting them. Each road connects two different parking spaces, and between any pair of parking spaces there can be at most one road. Each parking space can hold at most one car at any time. Other vehicles cannot drive through an occupied parking space。

$$\text{Figure 1: Rules for constructing a parking graph, corresponding to the three points below}$$

A series-parallel parking lot (also called a splot; hereafter called a parking graph) is a parking lot with two special parking spaces called the source and the sink. It is built from a single parking space using the series-parallel composition rules. Each parking graph can be specified by an encoding that describes its structure and the positions of parked cars. The encoding is a character sequence. Valid parking graphs and their encodings are defined recursively as follows:

  1. A parking lot containing only one parking space and no roads is a valid parking graph. This single parking space is both the source and the sink of the parking graph. If the parking space is empty, the encoding is the lowercase letter o; if it is occupied by a car, the encoding is the lowercase letter x

  2. If G1G_1 and G2G_2 are two valid parking graphs, then their series composition GG is also a parking graph. The series composition is obtained by adding a road between the sink of G1G_1 and the source of G2G_2. The source of the new parking graph GG is the source of G1G_1, and the sink is the sink of G2G_2. If E1E_1 and E2E_2 are the encodings of parking graphs G1G_1 and G2G_2, respectively, then the encoding of GG is SE1E2#. In other words, the encoding is obtained by concatenating the uppercase letter S, the encodings of the component parking graphs, and the hash symbol #

  3. If G1G_1 and G2G_2 are two valid parking graphs, then their parallel composition GG is also a valid parking graph. The parallel composition is obtained by adding two new parking spaces ss and tt, adding roads from ss to the sources of G1G_1 and G2G_2, and adding roads from tt to the sinks of G1G_1 and G2G_2. The source of the new parking graph GG is the newly added parking space ss, and the sink of GG is the newly added parking space tt. If E1E_1 and E2E_2 are the encodings of parking graphs G1G_1 and G2G_2, and EsE_s and EtE_t are the encodings of the source ss and the sink tt (the lowercase letter o if the corresponding space is empty, otherwise the lowercase letter x), then the encoding of GG is PEs|E1E2|Et#. In other words, the encoding is obtained by concatenating the uppercase letter P, the encoding of the source parking space, the vertical bar |, the encodings of the component parking graphs, another vertical bar |, the encoding of the sink parking space, and finally the hash symbol #

$$\text{Figure 2: The parking graph corresponding to the first sample test below}$$

For example, the encoding of the parking graph shown above is Po|Px|Sxo#Soo#|o#Soo#|o# (translator’s note: {Po|[Px|(Sxo#)(Soo#)|o#](Soo#)|o#})。Note that in the encoding of a parking graph GG, the number of lowercase letters (o or x) is always equal to the number of parking spaces in GG, and there is a one-to-one correspondence between the parking spaces in the parking graph and the lowercase letters in its encoding. The parking lot has only one exit, located at the source parking space of the entire parking graph. If a car can reach the source parking space through some roads and empty parking spaces (that is, it can leave the parking graph), then we say the car is not blocked. For example, in the parking graph above, neither car is blocked, but if we park a car at the sink of the parking graph (the rightmost node), then one of the cars will be blocked. It is allowed to park a car at the source parking space of the parking graph (but if you do so, all other cars in the parking graph will be blocked)。

Problem Description

The parking lot operator wants to arrange incoming cars in such a way that no car in the graph is blocked。

Write a program that computes the maximum total number of cars that can be parked in the given parking lot (including the cars already there), such that no car is blocked, and without moving any car that is already there. In addition, the program should find a way to park the maximum number of cars in the parking graph。

Input Format

The input consists of one line containing a sequence of at least 11 and at most 10510^5 characters, representing the encoding of the given splot. The sequence contains only uppercase letters P and S, lowercase letters o and x, and the characters # (ASCII 35) and | (ASCII 124). According to the rules above, the input will be an encoding of a splot. The input guarantees that the cars already in the parking lot are not blocked。

Output Format

The output should contain 22 lines. The first line should contain an integer mm, the maximum number of cars that can be parked in the splot。

The second line should contain a character sequence describing an optimal final arrangement of cars in the splot. This sequence must contain exactly mm letters x, and it should be obtained by replacing some of the original letters o with x

There may be multiple optimal arrangements, and the program may output any of them。

Po|Px|Sxo#Soo#|o#Soo#|o#
3
Po|Px|Sxo#Sox#|o#Soo#|o#
Po|SPo|oo|o#Px|oo|o##Po|Sxo#Po|ox|o#|o#|o#
7
Po|SPo|xx|o#Px|ox|o##Po|Sxx#Po|ox|o#|o#|o#

Hint

See the sample explanation in the last part of the Description。

If the output is incorrect or incomplete, but the first line of output (the maximum number of cars) is correct, you will receive 80%80\% of the score for the corresponding test points。

In 30%30\% of the test points, the total number of parking spaces in the splot is at most 2020

In another 40%40\% of the test points, the splot is empty, i.e. the input contains no letter x

For 100%100\% of the testdata, the encoding of the given splot contains at most 10510^5 characters。

SPJ provider:

https://www.luogu.com.cn/user/542457

Translated by ChatGPT 5