atcoder#AGC048A. [AGC048A] atcoder < S
[AGC048A] atcoder < S
Score : points
Problem Statement
Given is a string consisting of lowercase English letters.
Snuke can do the operation of swapping two adjacent characters in .
For example, if 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
$ holds lexicographically.
The definition of the lexicographic relation $x< y$
For strings and , it is said that holds lexicographically if and only if one of the following conditions is satisfied:
- There exists an integer () such that the first characters of and those of are equal and the -th character of is strictly smaller than that of in alphabetical order.
- holds, and the first characters of are equal to .
Solve test cases in an input file.
Constraints
- 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:
Then, test cases follow, each of which is in the following format:
Output
For each test case, print if the objective is unachievable, and print the minimum number of operations needed if it is achievable. Use one line for each case.
3
atcodeer
codeforces
aaa
1
0
-1
- The first test case: for example, swapping the last two characters makes
atcodere
atcoder
hold. - The second test case: before doing any operation, we already have
codeforces
atcoder
. - The third test case: no sequence of operations makes
atcoder
hold.