(ox)

AtCoder
IDagc054_d
Time2000ms
Memory256MB
Difficulty
Given is a string $S$ consisting of `(`, `)`, `o`, and `x`. You can swap two adjacent characters in $S$ any number of times. Find the minimum number of swaps needed to satisfy the following condition. * Let us make a string $S'$ consisting of `(` and `)` by replacing every occurrence of `o` with `()` and every occurrence of `x` with `)(` in $S$. Then, $S'$ is a **balanced parenthesis string**. Definition of a balanced parenthesis stringA balanced parenthesis string is a string that is one of the following: * an empty string; * a string that is a concatenation of $s$, $t$ in this order, where $s$ and $t$ are some non-empty balanced parenthesis strings; * a string that is a concatenation of `(`, $s$, `)` in this order, where $s$ is some balanced parenthesis string. Under the Constraints of this problem, it is always possible to achieve the objective. ## Constraints * $S$ is a string consisting of `(`, `)`, `o`, and `x`. * $S$ contains the same non-zero number of `(`s and `)`s. * $|S| \leq 8000$ ## Input Input is given from Standard Input in the following format: $S$ [samples]
Samples
Input #1
)x(
Output #1
3

We should do as follows: `)x(` → `x)(` → `x()` → `(x)`. Here, we have $S'=$`()()`, which is a balanced parenthesis string.
Input #2
()ox
Output #2
2
Input #3
()oxo(xxx))))oox((oooxxoxo)oxo)ooo(xxx(oox(x)(x()x
Output #3
68
API Response (JSON)
{
  "problem": {
    "name": "(ox)",
    "description": {
      "content": "Given is a string $S$ consisting of `(`, `)`, `o`, and `x`. You can swap two adjacent characters in $S$ any number of times. Find the minimum number of swaps needed to satisfy the following condition.",
      "description_type": "Markdown"
    },
    "platform": "AtCoder",
    "limit": {
      "time_limit": 2000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "agc054_d"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "Given is a string $S$ consisting of `(`, `)`, `o`, and `x`. You can swap two adjacent characters in $S$ any number of times. Find the minimum number of swaps needed to satisfy the following condition....",
      "is_translate": false,
      "language": "English"
    }
  ]
}
Full JSON Raw Segments