P18099 [CCPC 2026 Changchun] No Three in a Row
Background
This problem is a **communication problem**.
Please make sure to flush the standard output buffer after every output. The ways to flush in various programming languages are as follows:
- For C/C++, use `fflush(stdout)` or `cout.flush()`;
- For Java, use `System.out.flush()`;
- For Python, use `sys.stdout.flush()`.
Description
Little L and Little S are a pair of very close friends.
One day, Little L sees a string $P$ of length $n$ that contains only the characters `0` and `1`, and she needs to pass the content of $P$ to Little S.
Little L has a typewriter, which can send a string $Q$ containing only the characters `0` and `1` to Little S. However, because the typewriter has been out of repair for a long time, the transmitted string $Q$ has the following two restrictions:
1. The length of $Q$ cannot exceed $\lfloor\frac 32n\rfloor$, where $\lfloor x\rfloor$ denotes the greatest integer not greater than $x$.
2. No three consecutive characters in $Q$ can be all the same, that is, there is no positive integer $1\le i \le m-2$ such that $Q_i=Q_{i+1}=Q_{i+2}$, where $m$ is the length of $Q$. The indices of the string start from $1$.
Please help Little L and Little S design a transmission strategy, so that Little S can deduce the string $P$ that Little L saw from the string $Q$ sent by Little L.
## Implementation Details
Your program will **run twice independently**. In the first run it plays the role of Little L and needs to send a string $Q$ satisfying the restrictions to Little S according to the string $P$ it sees; in the second run it plays the role of Little S and needs to deduce the string $P$ that Little L saw from the received string $Q$. The interactor will pass information between your two programs.
In the first run, your program will first receive the length and the content of the string $P$, and then your program needs to output the length and the content of the string $Q$, so as to pass information to the second run.
In the second run, your program will receive from the interactor the information from the first run, that is, the length and the content of the string $Q$. You need to deduce the content of the corresponding string $P$ from this information.
Input Format
Each test point contains multiple test cases. The first line of the input contains two positive integers $T,r$ ($1\le T \le 2\times 10^4$, $r\in \{1,2\}$), representing the number of test cases and the stage number. For each test case:
- If $r=1$, the input format is as follows:
- The first line contains a positive integer $n$ ($1\le n \le 5\times 10^5$), representing the length of the string $P$.
- The second line contains a string $P$ of length $n$ that contains only the characters `0` and `1`.
You need to output two lines in the following format:
- The first line contains a positive integer $m$ ($1\le m \le \lfloor\frac 32n\rfloor$), representing the length of the string $Q$.
- The second line contains a string $Q$ of length $m$ that contains only the characters `0` and `1`, and no three consecutive characters in it are all the same.
For each test case, you must output the string $Q$ and flush the standard output buffer before you can continue reading the next test case.
It is guaranteed that the sum of $n$ over all testdata in a single test point does not exceed $5\times 10^5$.
- If $r=2$, the input format is as follows:
- The first line contains a positive integer $m$, representing the length of the string $Q$.
- The second line contains a string $Q$ of length $m$ that contains only the characters `0` and `1`.
You need to output two lines in the following format:
- The first line contains a positive integer $n$, representing the length of the string $P$.
- The second line contains a string $P$ of length $n$ that contains only the characters `0` and `1`, representing the string that Little L saw.
For each test case, you must output the string $P$ and flush the standard output buffer before you can continue reading the next test case. Moreover, the order in which the testdata are given within a single test point **may be different** from that in the first run.
Output Format
N/A
Explanation/Hint
## Sample
In the first run, the input is as follows:
```text
3 1
1
0
3
111
6
001100
```
Little L may send the strings with the following output:
```text
1
0
4
0010
7
0101010
```
According to the above output, in the second run, one possible input is as follows:
```text
3 2
4
0010
7
0101010
1
0
```
By telepathy, Little S deduces the $P$ of the three test cases and outputs them in the following format:
```text
3
111
6
001100
1
0
```
Note that in the second test case of the first run, Little L cannot send the string $Q$ in the following ways:
```text
5
10101
```
The length $m=5$ of $Q$ is greater than $\lfloor \frac 32n\rfloor=4$, which violates restriction 1.
```text
4
1110
```
The first three characters $Q_1,Q_2,Q_3$ of $Q$ are all `1`, which violates restriction 2.
## How to Test Your Program
The provided script file `triplet_testing_tool.py` can help you test locally and print the detailed interaction process. When testing, put the script file and the executable file you compiled in the same folder, and then run the following command in the terminal:
```bash
python3 triplet_testing_tool.py [--quiet]
```
Here:
- `-q, --quiet` is an optional parameter. With this parameter, the testing tool will not print the detailed interaction process.
- `data_file` is the path of the input data file you provide to the testing script. This file must satisfy the following format:
- The first line contains two non-negative integers $T,seed$, representing the number of test cases and the random seed used to shuffle the testdata respectively. If $seed=0$ is specified, it means no shuffling is performed.
- The format of each test case is exactly the same as the input format of the "first run" described above.
- `program` is the path of the executable file you compiled.
- `arguments` are the additional command-line arguments passed to this executable file.
For more implementation details of the testing script, please refer to its source code directly.
**Notes**:
1. The implementation of this testing tool is **not exactly the same** as that of the interactor used in the actual judging. The local testing result is not final and is only for reference during debugging.
2. The testing tool **does not monitor** the time and memory consumption of your program, so it cannot be used to test whether your program meets the time and memory limits of the problem.
Download the local testing tool: [triplet_testing_tool.py](attachments/I/triplet_testing_tool.py).
Translated by QwenWork