#P1139. 单向双轨道
单向双轨道
Description
As shown in the figure, a rail yard has two shunting stations B and C. At the left entrance A, there are trains waiting to enter (from left to right labeled ), and on the right side is the exit D. On this segment, trains entering from A and passing through B and C can only move one-way from left to right, and shunting stations B and C have no limit on the number of cars they can hold.

You are given and a permutation of lowercase letters, which represents the sequence of train IDs from left to right formed at exit D. Output a sequence of operations, one per line, each being a letter sequence of the form , where is a train ID, is the original position of (positions are denoted by ), and is the new position. Otherwise, output NO to indicate that such a dispatch cannot be completed.
Input Format
An integer () and a string consisting of lowercase letters.
Output Format
If it is feasible, output the shortest dispatch sequence. When there are multiple solutions, output the lexicographically smallest scheme with respect to the operation . If it is not feasible, output NO.
3
cba
c A B
b A B
a A D
b B D
c B D
Hint
Translated by ChatGPT 5
京公网安备 11011102002149号