QOJ.ac

QOJ

Time Limit: 3 s Memory Limit: 256 MB Total points: 100

#23831. Tower of JOIOI

Statistics

Problem Statement

Tower of JOIOI is a single-player game played using disks.

This game is played using several disks, each having one of the characters J, O, or I written on it. The disks have mutually different diameters, and at the start of the game, these disks are stacked from bottom to top in descending order of diameter. Using these disks, you want to make as many mini-JOIOI towers as possible. A mini-JOIOI tower consists of $3$ disks, and reads JOI or IOI when read in ascending order of disk diameter. However, the same disk cannot be used $2$ or more times.

Figure

Figure: $2$ mini-JOIOI towers can be made from JOIOII

Task

The characters written on the prepared disks are given as a string $S$ of length $N$ in ascending order of disk diameter. Write a program to find the maximum number of mini-JOIOI towers that can be made using these disks.

Limits

  • $1 \le N \le 1000000$: The length of the string $S$.

Input

Read the following data from standard input.

  • On the $1$-st line, an integer $N$ is written. $N$ represents the length of the string $S$.
  • On the $2$-nd line, a string $S$ is written.

Output

Output to standard output on $1$ line an integer representing the maximum number of mini-JOIOI towers that can be made.

Subtasks

  • For $10\%$ of the points in the grading data, $N \le 15$ is satisfied.
  • For $30\%$ of the points in the grading data, $N \le 50$ is satisfied.
  • For $50\%$ of the points in the grading data, $N \le 3000$ is satisfied.

Sample 1

Input

6
JOIIOI

Output

2

JOIIOI contains JOI and IOI as subsequences $1$ time each, so $2$ mini-JOIOI towers can be made.

Sample 2

Input

5
JOIOI

Output

1

Although it contains JOI and IOI as subsequences, a character cannot be used $2$ or more times, so they cannot be extracted simultaneously.

Sample 3

Input

6
JOIOII

Output

2

This sample corresponds to the example in the problem statement.

Sample 4

Input

15
JJOIIOOJOJIOIIO

Output

4

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.