아직 만들고 있는 페이지입니다.

이 페이지는 아직 만드는 중입니다. 보이는 내용은 바뀔 수 있습니다.

Moo Operations

시간 제한2초메모리 제한1024 MB

요약
M과 O로 이루어진 각 문자열에서 양 끝을 지우거나 뒤집어 최소 횟수로 "MOO"를 만들고, 불가능하면 -1을 출력한다.
난이도

보통10점 중 4점

유형
문자열, 그리디
정답자
아직 제출이 없습니다

문제

Because Bessie is bored of playing with her usual text string where the only characters are 'C,' 'O,' and 'W,' Farmer John gave her QQ new strings (1≤Q≤1001 \leq Q \leq 100), where the only characters are 'M' and 'O.' Bessie's favorite word out of the characters 'M' and 'O' is obviously "MOO," so she wants to turn each of the QQ strings into "MOO" using the following operations:

  1. Replace either the first or last character with its opposite (so that 'M' becomes 'O' and 'O' becomes 'M').
  2. Delete either the first or last character.

Unfortunately, Bessie is lazy and does not want to perform more operations than absolutely necessary. For each string, please help her determine the minimum number of operations necessary to form "MOO" or output −1-1 if this is impossible.

입력

The first line of input contains the value of QQ.

The next QQ lines of input each consist of a string, each of its characters either 'M' or 'O'. Each string has at least 1 and at most 100 characters.

출력

Output the answer for each input string on a separate line.

힌트

A sequence of 44 operations transforming the first string into "MOO" is as follows:

Replace the last character with O (operation 1)
Delete the first character (operation 2)
Delete the first character (operation 2)
Delete the first character (operation 2)

The second string cannot be transformed into "MOO." The third string is already "MOO," so no operations need to be performed.

예제1

  1. 예제 1

    입력
    3
    MOMMOM
    MMO
    MOO
    
    예상 출력
    4
    -1
    0