#P1139. Train Station Scheduling
Train Station Scheduling
Train Station Scheduling
At a train station as illustrated, there are two dispatch stations B and C. At the left entrance A, there are n trains waiting to enter the station. They are labeled from left to right as a, b, c, .... On the right side is the exit D. Trains travel only from left to right through the stations A → B → C → D, and there is no limit to the number of trains that can be held at B and C.
From the input file, you are given an integer n and a permutation of n lowercase letters. This permutation represents the sequence of train identifiers (from left to right) as they appear at exit D. Your task is to output a series of operations to achieve this rearrangement. Each operation is a line in the format h, L, R
, where h
is the train identifier, L
is the current station (one of A, B, C, or D), and R
is the station to which the train moves. If it is impossible to achieve the target sequence, output NO
.
inputFormat
The first line contains an integer n (1 ≤ n ≤ 26). The second line contains a permutation of n distinct lowercase letters (starting from 'a') representing the target order of trains at station D (from left to right).
outputFormat
If the required scheduling is possible, output a series of operations, one per line, where each line is in the format h, L, R
(without extra spaces), indicating that train h
moves from station L
to station R
. Otherwise, output a single line with NO
.
sample
4
abcd
a, A, B
a, B, C
a, C, D
b, A, B
b, B, C
b, C, D
c, A, B
c, B, C
c, C, D
d, A, B
d, B, C
d, C, D
</p>