#63201. [USACO 2023 Jan Bronze]Moo Operations

[USACO 2023 Jan Bronze]Moo Operations

暂无测试数据。

Because Bessie is bored of playing with her usual text string where the only characters are 'C,' 'O,' and 'W,' Farmer John gave her Q new strings (1≤Q≤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 Q 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 if this is impossible.

因为贝西厌倦了玩只有“C”、“O”和“W”的文本字符串游戏,农民约翰给了她Q个新的字符串(1≤Q≤100),其中只有 'M''O'。在字母 'M''O' 中,贝西最喜欢的单词显然是 “MOO”,所以她想用下面的操作把每个Q字符串都变成 “MOO”:

  1. 将第一个或最后一个字符替换为它的反义词(这样 'M' 就变成 'O''O' 就变成 'M')。
  2. 删除第一个或最后一个字符。

不幸的是,贝西很懒,除非必要,她不想做更多的操作。对于每个字符串,请帮助她确定形成 “MOO” 所需的最小操作数,如果不能形成,则输出 -1

输入格式

The first line of input contains the value of Q.

The next Q 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.

第一行输入包含Q的值。

接下来的Q行输入都由一个字符串组成,每个字符串的字符都是 'M' 或 'O'。每个字符串至少 $1$ 个字符,最多100个字符。

输出格式

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

在单独的一行上输出每个输入字符串的答案。

数据范围

Inputs 2-4: Every string has length at most 3.

Inputs 5-11: No additional constraints.

3
MOMMOM
MMO
MOO
4
-1
0