Antiparticle Antiphysics

시간 제한2초메모리 제한1024 MB

요약
문자열 S와 E가 주어졌을 때 P를 APA로, A를 PAP로 바꾸고 a개의 연속 A 또는 p개의 연속 P를 지우는 연산으로 S를 E로 만들 수 있는지 판정하고 연산 순서를 출력한다.
난이도

어려움10점 중 8점

유형
문자열, 그리디, 구현
정답자
아직 제출이 없습니다

문제

In an alternate universe, where the laws of physics are out of whack...

A new research facility has just been built. It is called the Large Antihadron Collider (LAC), the largest antiparticle collider of its kind, and antiphysicists are eager to use it to study something called “regular matter”, which is similar to antimatter except with reversed charge, parity, and time.

In one of their LAC experiments, the antiphysicists successfully confined two kinds of particles, antiprotons and protons, in a container, where particles are lined up from left to right. We can represent the container’s state as a 11-indexed string. The length of the string equals the number of particles in the container, and the ii-th character of the string is A if the ii-th particle from the left is an antiproton, or P if it is a proton.

Using the LAC’s bizarro-energy beams, they can modify the state using any of four different types of operations:

  • Operation 1: Choose a particular proton, and then insert two antiprotons, one to its left and the other to its right. This has the effect of replacing the corresponding character P in the state string with APA.
  • Operation 2: Choose a particular antiproton, and then insert two protons, one to its left and the other to its right. This has the effect of replacing the corresponding character A in the state string with PAP.
  • Operation 3: Choose a contiguous subsequence of aa antiprotons, and then remove them.
  • Operation 4: Choose a contiguous subsequence of pp protons, and then remove them.

Note that the integers aa in Operation 3 and pp in Operation 4 are given in the input and are fixed.

These operations can be performed an arbitrary number of times in an arbitrary order, but only one operation can be performed at a time.

The initial state is represented by the string SS. They would like to transform it into the goal state represented by the string EE using a sequence of operations. Determine whether it is possible to do so. If it is possible, find one sequence of operations that transforms the initial state into the goal state.

입력

The first line of input contains one integer tt (1≤t≤101 ≤ t ≤ 10) representing the number of test cases. After that, tt test cases follow. Each of them consists of a single line containing two integers aa and pp (5≤a,p≤205 ≤ a, p ≤ 20) and two strings SS and EE (1≤∣S∣,∣E∣≤501 ≤ |S|, |E| ≤ 50, S≠ES \ne E). The strings SS and EE consist only of characters AA and PP.

출력

For each test case, output the following.

If the transformation is impossible, output -1 in a single line.

Otherwise, on the first line, output an integer kk representing the number of operations to transform the initial state into the goal state. On each of the next kk lines, output one of the following, without the quotes, to describe one operation:

  1. “+P ii” to apply Operation 1 on the ii-th particle from the left (i≥1i ≥ 1). This particle must be a proton.
  2. “+A ii” to apply Operation 2 on the ii-th particle from the left (i≥1i ≥ 1). This particle must be an antiproton.
  3. “-A ii” to apply Operation 3 on the aa consecutive particles whose leftmost particle is the ii-th particle from the left (i≥1i ≥ 1). These particles must be antiprotons.
  4. “-P ii” to apply Operation 4 on the pp consecutive particles whose leftmost particle is the ii-th particle from the left (i≥1i ≥ 1). These particles must be protons.

These operations are performed in the order of the output lines and must transform the initial state into the goal state.

The number of operations kk must satisfy 1≤k≤35,0001 ≤ k ≤ 35\\, 000. It can be shown that there’s always a sequence of operations satisfying this bound for kk if the initial state can be transformed into the goal state at all. Any valid sequence satisfying this bound for kk will be accepted. In particular, you do not need to minimize the value of kk.

예제1

  1. 예제 1

    입력
    4
    13 10 PP PAAAAPAAAA
    10 13 AAAAAAA PPPPPPP
    7 8 PPAAAAAAAAP PPAP
    8 9 PAPPPPPPPPP PPAP
    
    예상 출력
    4
    +P 2
    +P 3
    +P 4
    +P 5
    -1
    1
    -A 3
    2
    +A 2
    -P 5