luogu#P7919. [Kubic] ABC

[Kubic] ABC

Background

It is recommended to read the background of Problem D first.

Problem Description

You are given a string SS of length nn that contains only A,B,C\texttt{A,B,C}. You may perform several operations. In each operation:

  • First choose an interval [l,r][l,r], and you must ensure that 1lrn1\le l\le r\le n.

  • Then choose three characters pA,pB,pC{A,B,C}pA,pB,pC\in\{\texttt{A,B,C}\}, and change all A\texttt{A} in SlrS_{l\dots r} into pApA, all B\texttt{B} into pBpB, and all C\texttt{C} into pCpC. pA,pB,pCpA,pB,pC may be equal.

Find the minimum number of operations needed to make any two adjacent characters in SS different, and output a construction.

Input Format

The first line contains an integer nn.

The second line contains a string SS of length nn.

Output Format

The first line contains an integer mm, the number of operations in your constructed plan.

The next mm lines each contain two integers l,rl,r and three characters pA,pB,pCpA,pB,pC.

You must ensure that 1lrn,pA,pB,pC{A,B,C}1\le l\le r\le n,pA,pB,pC\in\{\texttt{A,B,C}\}.

Note: There is no need to, and you should not, add spaces between pA,pB,pCpA,pB,pC (see the sample output).

5
ABBAA
1
3 4 BAC
5
ABCBA
0

Hint

For 100%100\% of the testdata, 1n5×103,Si{A,B,C}1\le n\le 5\times 10^3,S_i\in\{\texttt{A,B,C}\}.

Constraints

Score nn Special Property
Subtask1\operatorname{Subtask}1 11 No special limits i[1,n),SiSi+1\forall i\in[1,n),S_i\neq S_{i+1}
Subtask2\operatorname{Subtask}2 1919 10\le 10 None
Subtask3\operatorname{Subtask}3 1010 No special limits Si=AS_i=\texttt{A}
Subtask4\operatorname{Subtask}4 2020 Si{A,B}S_i\in\{\texttt{A,B}\}
Subtask5\operatorname{Subtask}5 100\le 100 None
Subtask6\operatorname{Subtask}6 3030 No special limits

Scoring

You will get 00 points on this test point if any of the following happens:

  • The output format does not meet the requirements.

  • You output extra information (including spaces and newline characters).

  • The number of operations in your constructed plan is different from the official answer.

  • Your constructed plan does not satisfy the problem requirements.

  • Time limit exceeded.

If none of the above happens, you will get full points on this test point.

It is guaranteed that the SPJ uses no more than 100ms,10MB100\operatorname{ms},10\operatorname{MB}.

Sample Explanation 1

One possible sequence of operations is:

ABBAA

ABABA

It can be proven that there is no better plan.

Sample Explanation 2

The initial sequence already satisfies the requirements, so you can directly output a single line 00.

Translated by ChatGPT 5