ABC String

AtCoder
IDagc036_e
Time2000ms
Memory256MB
Difficulty
Given is a string $S$ consisting of `A`,`B`, and `C`. Consider the (not necessarily contiguous) subsequences $x$ of $S$ that satisfy all of the following conditions: * `A`, `B`, and `C` all occur the same number of times in $x$. * No two adjacent characters in $x$ are the same. Among these subsequences, find one of the longest. Here a subsequence of $S$ is a string obtained by deleting zero or more characters from $S$. ## Constraints * $1 \leq |S| \leq 10^6$ * $S$ consists of `A`,`B`, and `C`. ## Input Input is given from Standard Input in the following format: $S$ [samples]
Samples
Input #1
ABBCBCAB
Output #1
ACBCAB

Consider the subsequence `ACBCAB` of $S$. 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.
Input #2
ABABABABACACACAC
Output #2
BABCAC
Input #3
ABCABACBCBABABACBCBCBCBCBCAB
Output #3
ACABACABABACBCBCBCBCA
Input #4
AAA
Output #4
It is possible that only the empty string satisfies the condition.
API Response (JSON)
{
  "problem": {
    "name": "ABC String",
    "description": {
      "content": "Given is a string $S$ consisting of `A`,`B`, and `C`. Consider the (not necessarily contiguous) subsequences $x$ of $S$ that satisfy all of the following conditions: *   `A`, `B`, and `C` all occur t",
      "description_type": "Markdown"
    },
    "platform": "AtCoder",
    "limit": {
      "time_limit": 2000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "agc036_e"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "Given is a string $S$ consisting of `A`,`B`, and `C`.\nConsider the (not necessarily contiguous) subsequences $x$ of $S$ that satisfy all of the following conditions:\n\n*   `A`, `B`, and `C` all occur t...",
      "is_translate": false,
      "language": "English"
    }
  ]
}
Full JSON Raw Segments