#P1139. 单向双轨道

单向双轨道

Description

As shown in the figure, a rail yard has two shunting stations B and C. At the left entrance A, there are nn trains waiting to enter (from left to right labeled a,b,c,da, b, c, d), 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 nn and a permutation of nn 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 h,L,Rh, L, R, where hh is a train ID, LL is the original position of hh (positions are denoted by A,B,C,D\verb!A,B,C,D!), and RR is the new position. Otherwise, output NO to indicate that such a dispatch cannot be completed.

Input Format

An integer nn (1<n151 < n \le 15) and a string consisting of nn 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 (L,R)(L, R). 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