#P6878. [JOI 2020 Final] JJOOII 2
[JOI 2020 Final] JJOOII 2
Description
A string made up of consecutive , followed by consecutive , followed by consecutive is called a -th order JOI string.
For example, is a -th order JOI string. However, note that the order matters. For example, is not a -th order JOI string.
Now, given a string of length , you may perform the following three operations:
- Operation 1: Delete the first character of .
- Operation 2: Delete the last character of .
- Operation 3: Delete one character of other than the first and the last.
We want to make become a -th order JOI string using these operations.
However, we want to use Operation 3 as few times as possible.
So, what is the minimum number of times Operation 3 must be performed to make into a -th order JOI string?
If it is impossible to make it into a -th order JOI string, output .
Input Format
The first line contains two integers , representing the string length and the order of the JOI string to construct.
The second line contains characters, representing the string .
Output Format
Output one integer: the minimum number of times Operation 3 needs to be performed.
If it is impossible to make it into a -th order JOI string, output .
10 2
OJIJOIOIIJ
2
9 3
JJJOOOIII
0
9 1
IIIOOOJJJ
-1
Hint
Explanation for Sample 1
- Perform Operation 1 once to get .
- Perform Operation 2 once to get .
- Perform Operation 3 once: delete character to get .
- Perform Operation 3 once: delete character to get .
Explanation for Sample 2
is already a -th order JOI string, so no operations are needed.
Explanation for Sample 3
cannot be turned into a -st order JOI string, so there is no solution.
Constraints
This problem uses bundled testdata.
- Subtask 1 (1 pts): .
- Subtask 2 (12 pts): .
- Subtask 3 (87 pts): No additional constraints.
For of the testdata:
- .
- .
- contains only , , , and has length .
Note
Translated from The 19th Japanese Olympiad in Informatics Final Round B JJOOII 2。
Translated by ChatGPT 5
京公网安备 11011102002149号