#P6878. [JOI 2020 Final] JJOOII 2

[JOI 2020 Final] JJOOII 2

Description

A string made up of KK consecutive J\tt J, followed by KK consecutive O\tt O, followed by KK consecutive I\tt I is called a KK-th order JOI string.

For example, JJOOII\tt JJOOII is a 22-th order JOI string. However, note that the order matters. For example, OOJJII\tt OOJJII is not a 22-th order JOI string.

Now, given a string SS of length NN, you may perform the following three operations:

  • Operation 1: Delete the first character of SS.
  • Operation 2: Delete the last character of SS.
  • Operation 3: Delete one character of SS other than the first and the last.

We want to make SS become a KK-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 SS into a KK-th order JOI string?

If it is impossible to make it into a KK-th order JOI string, output 1-1.

Input Format

The first line contains two integers N,KN, K, representing the string length and the order KK of the JOI string to construct.
The second line contains NN characters, representing the string SS.

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 KK-th order JOI string, output 1-1.

10 2
OJIJOIOIIJ
2
9 3
JJJOOOIII

0
9 1
IIIOOOJJJ

-1

Hint

Explanation for Sample 1

  1. Perform Operation 1 once to get JIJOIOIIJ\tt JIJOIOIIJ.
  2. Perform Operation 2 once to get JIJOIOII\tt JIJOIOII.
  3. Perform Operation 3 once: delete character 22 to get JJOIOII\tt JJOIOII.
  4. Perform Operation 3 once: delete character 44 to get JJOOII\tt JJOOII.

Explanation for Sample 2

JJJOOOIII\tt JJJOOOIII is already a 33-th order JOI string, so no operations are needed.

Explanation for Sample 3

IIIOOOJJJ\tt IIIOOOJJJ cannot be turned into a 11-st order JOI string, so there is no solution.

Constraints

This problem uses bundled testdata.

  • Subtask 1 (1 pts): N21N \le 21.
  • Subtask 2 (12 pts): N3000N \le 3000.
  • Subtask 3 (87 pts): No additional constraints.

For 100%100\% of the testdata:

  • 3N2×1053 \le N \le 2 \times 10^5.
  • 1KN31 \le K \le \dfrac{N}{3}.
  • SS contains only J\tt J, O\tt O, I\tt I, and has length NN.

Note

Translated from The 19th Japanese Olympiad in Informatics Final Round B JJOOII 2

Translated by ChatGPT 5