JOI Logo Design
Time limit1sMemory limit512 MB
Given a circular string of length 4^K over {J,O,I}, find the rotation that minimizes mismatches with a level-K JOI sequence built by recursive quarter block replacement.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Divide and conquer, String, Brute force
- Solved
- No attempts yet
Problem
Chairman K has decided to design a logo for merchandise supporting the Japanese Olympiad in Informatics contestants. One day Chairman K hit on the idea of arranging the characters 'J', 'O', and 'I' in a circle as the logo. The idea carries a wish for people to enjoy JOI.
For an integer , a level JOI sequence is defined as follows.
- A level 0 JOI sequence is a string of length 1 consisting of one of the characters 'J', 'O', and 'I'.
- A level JOI sequence is a string of length whose first characters are all 'J', whose next characters are all 'O', whose next characters are all 'I', and whose last characters form a level JOI sequence.
Chairman K now has a sheet of paper with characters written in a circle. Each character on the paper is one of 'J', 'O', and 'I'. Chairman K will rewrite some of the characters so that, when the characters on the paper are read clockwise around the circle starting from some character, they form a level JOI sequence. He wants to keep the number of rewritten characters as small as possible.
Given a string of length written in a circle on the paper, write a program that finds the minimum number of characters that must be rewritten so that reading the string clockwise around the circle from some starting character yields a level JOI sequence.
Input
Read the following data from standard input.
- The first line contains an integer . This means that characters are written in a circle on the paper.
- The second line contains a string of length consisting of 'J', 'O', and 'I'. This means that reading the characters on the paper clockwise around the circle starting from some character yields this string.
Output
Print the minimum number of characters Chairman K rewrites on a single line to standard output.
Constraints
- .