Jason ABC

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

요약
길이 3n인 A, B, C 문자열에서 구간을 한 문자로 덮어쓰는 연산을 최소로 사용해 각 문자가 n번씩 나오게 만드는 최적 연산 열을 구한다.
난이도

보통10점 중 7점

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

문제

You are given a string SS of length 3n3n, consisting of the characters A, B and C. You are allowed to perform the following operation:

  • Select some subsegment of this string and a character cc (one of A, B and C). Then, replace all the characters on the subsegment with cc.

Find the smallest number of times that you would have to apply the operation above to get a string which contains each of characters A, B and C exactly nn times. It can be shown that it is always possible to get such a string.

In addition, find a sequence of operations of the smallest possible length. If there are many such sequences, you can output any of them.

입력

The first line of input contains a single integer nn (1≤n≤3⋅1051 \le n \le 3 \cdot 10^5).

The second line of the input contains a string SS of length 3n3n, consisting of the characters A, B and C.

출력

In the first line print the minimum number of operations kk.

In the ii-th of the next kk lines print 22 integers l_i,r_il\_i, r\_i and a character c_ic\_i (1≤l_i≤r_i≤3n1 \le l\_i \le r\_i \le 3n, c\_i \in \\{A, B, C\\}), denoting that in the ii-th operation you will replace each of the characters S_l_i,S_l_i+1,…,S_r_iS\_{l\_i}, S\_{l\_i+1}, \ldots, S\_{r\_i} with c_ic\_i.

If there is more than one solution with a minimum number of operations, you can print any one of them.

힌트

In the first sample, the string will undergo the following transformations:

AAA →\to ABB →\to ABC.

In the second sample, the string already contains exactly one A, one B and one C.

In the third sample, the string will undergo the following transformation:

ABABCABAB →\to CCABCABAB. Now, it contains each letter 33 times.

예제3

  1. 예제 1

    입력
    1
    AAA
    
    예상 출력
    2
    2 3 B
    3 3 C
    
  2. 예제 2

    입력
    1
    CAB
    
    예상 출력
    0
    
  3. 예제 3

    입력
    3
    ABABCABAB
    
    예상 출력
    1
    1 2 C