#C10500. Taco Transformation

    ID: 39713 Type: Default 1000ms 256MiB

Taco Transformation

Taco Transformation

You are given an initial string \( s \) of length \( n \) and a target string \( t \) of the same length. In addition, there are \( m \) stages of operations defined on the string.

For each stage, you are provided with a sequence of indices. The operation for that stage is as follows: extract the characters at the given indices (in the order provided), reverse the extracted subsequence, and then place the reversed subsequence back at the same positions in \( s \). Your task is to determine whether you can transform the initial string \( s \) into the target string \( t \) after performing all the stages in order.

If after executing all \( m \) stages the string becomes equal to \( t \), output POSSIBLE; otherwise, output IMPOSSIBLE.

Note: The indices in each stage are provided in 1-indexed format.

inputFormat

The input is given from stdin in the following format:

  1. The first line contains two integers \( n \) and \( m \), where \( n \) is the length of the strings and \( m \) is the number of stages.
  2. The second line contains the initial string \( s \) of length \( n \).
  3. The third line contains the target string \( t \) of length \( n \).
  4. The following \( m \) lines each describe a stage. Each of these lines begins with an integer \( q_i \) indicating the number of indices for that stage, followed by \( q_i \) integers representing the 1-indexed positions.

outputFormat

Output a single line to stdout containing either POSSIBLE if the transformation is achievable, or IMPOSSIBLE if it is not.

## sample
5 2
abcde
acbde
2 2 3
1 4
POSSIBLE