atcoder#AGC036E. [AGC036E] ABC String

[AGC036E] ABC String

Score : 15001500 points

Problem Statement

Given is a string SS consisting of A,B, and C.

Consider the (not necessarily contiguous) subsequences xx of SS that satisfy all of the following conditions:

  • A, B, and C all occur the same number of times in xx.
  • No two adjacent characters in xx are the same.

Among these subsequences, find one of the longest. Here a subsequence of SS is a string obtained by deleting zero or more characters from SS.

Constraints

  • 1S1061 \leq |S| \leq 10^6
  • SS consists of A,B, and C.

Input

Input is given from Standard Input in the following format:

SS

Output

Print one longest subsequence that satisfies the conditions. If multiple solutions exist, any of them will be accepted.

ABBCBCAB
ACBCAB

Consider the subsequence ACBCAB of SS. It satisfies the conditions and is one of the longest with these properties, along with ABCBCA. On the other hand, the subsequences ABCBCAB and ABBCCA do not satisfy the conditions.

ABABABABACACACAC
BABCAC
ABCABACBCBABABACBCBCBCBCBCAB
ACABACABABACBCBCBCBCA
AAA

It is possible that only the empty string satisfies the condition.