Antiparticle Antiphysics
시간 제한2초메모리 제한1024 MB
문자열 S와 E가 주어졌을 때 P를 APA로, A를 PAP로 바꾸고 a개의 연속 A 또는 p개의 연속 P를 지우는 연산으로 S를 E로 만들 수 있는지 판정하고 연산 순서를 출력한다.
문제
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 -indexed string. The length of the string equals the number of particles in the container, and the -th character of the string is A if the -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
Pin the state string withAPA. - 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
Ain the state string withPAP. - Operation 3: Choose a contiguous subsequence of antiprotons, and then remove them.
- Operation 4: Choose a contiguous subsequence of protons, and then remove them.
Note that the integers in Operation 3 and 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 . They would like to transform it into the goal state represented by the string 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 () representing the number of test cases. After that, test cases follow. Each of them consists of a single line containing two integers and () and two strings and (, ). The strings and consist only of characters and .
출력
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 representing the number of operations to transform the initial state into the goal state. On each of the next lines, output one of the following, without the quotes, to describe one operation:
- “
+P” to apply Operation 1 on the -th particle from the left (). This particle must be a proton. - “
+A” to apply Operation 2 on the -th particle from the left (). This particle must be an antiproton. - “
-A” to apply Operation 3 on the consecutive particles whose leftmost particle is the -th particle from the left (). These particles must be antiprotons. - “
-P” to apply Operation 4 on the consecutive particles whose leftmost particle is the -th particle from the left (). 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 must satisfy . It can be shown that there’s always a sequence of operations satisfying this bound for if the initial state can be transformed into the goal state at all. Any valid sequence satisfying this bound for will be accepted. In particular, you do not need to minimize the value of .