atcoder < S

AtCoder
IDagc048_a
Time2000ms
Memory256MB
Difficulty
Given is a string $S$ consisting of lowercase English letters. Snuke can do the operation of swapping two **adjacent** characters in $S$. For example, if $S=$`agc`, one operation can change it to `gac` (by swapping `a` and `g`) or `acg` (by swapping `g` and `c`). Snuke wants to do this operation zero or more times so that `atcoder` $<S$ holds lexicographically. The definition of the lexicographic relation $x< y$For strings $x$ and $y$, it is said that $x< y$ holds lexicographically if and only if one of the following conditions is satisfied: * There exists an integer $i$ ($1 \leq i \leq \mathrm{min}(|x|,|y|)$) such that the first $i-1$ characters of $x$ and those of $y$ are equal and the $i$\-th character of $x$ is strictly smaller than that of $y$ in alphabetical order. * $|x|< |y|$ holds, and the first $|x|$ characters of $y$ are equal to $x$. Determine whether the objective is achievable. If it is achievable, find the minimum number of operations needed. Solve $T$ test cases in an input file. ## Constraints * $1 \leq T \leq 100$ * $1 \leq |S| \leq 1000$ * $S$ is a string consisting of lowercase English letters. ## Input Input is given from Standard Input in the following format. The first line of input is as follows: $T$ Then, $T$ test cases follow, each of which is in the following format: $S$ [samples]
Samples
Input #1
3
atcodeer
codeforces
aaa
Output #1
1
0
-1

*   The first test case: for example, swapping the last two characters makes $S=$`atcodere` $>$ `atcoder` hold.
*   The second test case: before doing any operation, we already have $S=$`codeforces` $>$ `atcoder`.
*   The third test case: no sequence of operations makes $S>$ `atcoder` hold.
API Response (JSON)
{
  "problem": {
    "name": "atcoder < S",
    "description": {
      "content": "Given is a string $S$ consisting of lowercase English letters. Snuke can do the operation of swapping two **adjacent** characters in $S$. For example, if $S=$`agc`, one operation can change it to `gac",
      "description_type": "Markdown"
    },
    "platform": "AtCoder",
    "limit": {
      "time_limit": 2000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "agc048_a"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "Given is a string $S$ consisting of lowercase English letters. Snuke can do the operation of swapping two **adjacent** characters in $S$. For example, if $S=$`agc`, one operation can change it to `gac...",
      "is_translate": false,
      "language": "English"
    }
  ]
}
Full JSON Raw Segments