#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。

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:
-
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 letterx。 -
If and are two valid parking graphs, then their series composition is also a parking graph. The series composition is obtained by adding a road between the sink of and the source of . The source of the new parking graph is the source of , and the sink is the sink of . If and are the encodings of parking graphs and , respectively, then the encoding of is
SE1E2#. In other words, the encoding is obtained by concatenating the uppercase letterS, the encodings of the component parking graphs, and the hash symbol#。 -
If and are two valid parking graphs, then their parallel composition is also a valid parking graph. The parallel composition is obtained by adding two new parking spaces and , adding roads from to the sources of and , and adding roads from to the sinks of and . The source of the new parking graph is the newly added parking space , and the sink of is the newly added parking space . If and are the encodings of parking graphs and , and and are the encodings of the source and the sink (the lowercase letter
oif the corresponding space is empty, otherwise the lowercase letterx), then the encoding of isPEs|E1E2|Et#. In other words, the encoding is obtained by concatenating the uppercase letterP, 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#。

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 , the number of lowercase letters (o or x) is always equal to the number of parking spaces in , 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 and at most 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 lines. The first line should contain an integer , 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 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 of the score for the corresponding test points。
In of the test points, the total number of parking spaces in the splot is at most 。
In another of the test points, the splot is empty, i.e. the input contains no letter x。
For of the testdata, the encoding of the given splot contains at most characters。
SPJ provider:
Translated by ChatGPT 5


