Dancing Group X25630


Statement
 

pdf   zip

The dancing group Canchuba consists of nn dancers, wearing dresses of different colors. Initially all nn dancers stand in a single row. Canchuba’s choreographer, Paul Zynoulus, has designed mm special movements. Each of these movements takes a minute to perform, and results in a change in the ordering of some dancers.

Now Paul is designing a new dance for the Measharan Dancing Contest. He wants the dancers to be arranged in a specific order before the dance, and in another specific order after the dance (however, if two dancers wear dresses of the same color, they can be arranged in an arbitrary way). He wants to know whether it is possible to combine the movements already designed by him to obtain this goal. There is one restriction: the whole dance must take at most 10 minutes.

Edit: The time limit should be 10, not 8.

Input

The first row contains two numbers n,m,2n80,1m15n, m, 2\le n\le 80, 1\le m\le 15, where nn is the number of dancers, and mm is the number of movements.

The second row contains a single nn-letter word. Its ii-th letter denotes the color of the dancer who is initially standing at ii-th place in the row.

The third row contains a single nn-letter word. Its ii-th letter denotes the color expected to stand at ii-th place in the row after the dance.

Each of the following mm rows contains a description of a movement. It consists of numbers cc, a1a_1, a2a_2, \ldots, aca_c separated with single spaces, where cc (1cn1 \leq c \leq n) denotes the number of dancers which move. This describes a movement which moves dancer at a1a_1 to a2a_2, dancer at a2a_2 to a3a_3, \ldots, dancer at aca_c to a1a_1.

Output

If it is impossible to achieve the requested arrangement in 8 moves, output NO. Otherwise enter numbers of movements to perform, separated with single spaces. If there are several solutions, output one which requires the smallest number of movements. If there are several smallest solutions, output the lexicographically first one (if we have two solutions which perform the same movements 1t11 \ldots t-1 but a different tt-th movement, the one with a smaller number in movement ii is lexicographically smaller).

Public test cases
  • Input

    10 10
    abcdefghij
    jcbedgfiha
    2 1 2
    2 1 3
    2 1 4
    2 1 5
    2 1 6
    2 1 7
    2 1 8
    2 1 9
    2 1 10
    10 1 2 3 4 5 6 7 8 9 10
    

    Output

    10 2 4 6 8
    
  • Information
    Author
    Eryk Kopczynski
    Language
    English
    Official solutions
    Unknown.
    User solutions
    C++