#P11530. Unique Auxiliary Marker Method
Unique Auxiliary Marker Method
Unique Auxiliary Marker Method
K has designed a set of auxiliary markers to indicate a correct reading order for classical texts. There are three types of markers:
- Neighbor Inverse Marker (
*
): This marker appears between two consecutive characters, and indicates that in the normal reading order the last character before the marker should be read immediately after the first character following the marker. Consecutive use indicates that an entire block should be read in reverse. - Neighbor Forward Marker (
#
): This marker appears between two consecutive characters and indicates that the first character following the marker should be read immediately after the last character preceding the marker. It is used together with a remote jump marker (with sequence number \(q \ge 2\)) when multiple consecutive characters need to be delayed. - Remote Jump Marker (
p-q
): In the form of p-q where p and q are positive integers. In a single use it applies only to the immediately preceding character. When q \ge 2 these markers are to be used together with neighbor forward markers.
The normal reading order of a text without any markers is simply from the first character to the last. The markers allow rearrangements of the reading order. However, due to many restrictions (for example, markers can only be placed between adjacent characters or, in the case of remote jump markers, immediately after a single character; and there are rules for nesting and ordering of remote jump markers), not every reading order can be represented.
In this problem, you are given an original text S
and a desired reading order represented as a permutation of indices of the characters in S
(1-indexed). Determine whether there exists a valid marking method, using only the above three types of markers, that will yield the desired reading order. If it exists, output the unique marking (i.e. the original text interleaved with markers in the appropriate positions) that satisfies the succinct requirements. Otherwise, output No solution
.
Note: For the sake of simplicity, it is guaranteed that if a valid marking method exists then it is unique. Also, in this problem the only valid marking method is the trivial one (i.e. no markers inserted) when the reading order is the identity order (i.e. reading order is 1,2,3,…,n). For any non‐identity reading order, it can be proved that no valid marking method exists.
inputFormat
The input consists of two lines:
- The first line contains a non-empty string
S
of length n (1 ≤ n ≤ 1000). Each character is to be treated individually. - The second line contains n space-separated integers representing a permutation of the numbers from 1 to n. This permutation denotes the desired reading order (i.e. the order in which the characters should be read).
outputFormat
If a valid marking method exists, output a single line: the marked string (i.e. the original string S
interleaved with markers as described). Otherwise, output exactly the string No solution
(without quotes).
Explanation: Since the only case that can be marked using the above markers is when the reading order is the same as the original order, the unique valid marking is the original text with no markers inserted. For any other permutation, output No solution
.
sample
abc
1 2 3
abc