boolcheckSubString(string a, string b){ int x = a.size(), y = b.size(); if (x <= y) { for (int i = 0; i < y; i++) { if (b[i] == a[0]) { bool flag = true; for (int j = 0; j < x; j++) if (b[i + j] == a[j]) continue; else { flag = false; break; } if (flag) returntrue; } } } returnfalse; }
intcheck(string a){ if (a.size() < 3) return-1; int step = 0; if (a == "MOO") return step; string tmp1 = a; step++; if (tmp1[0] == 'M') tmp1[0] = 'O'; else tmp1[0] = 'M'; if (tmp1 != "MOO") { step++; if (tmp1[2] == 'M') tmp1[2] = 'O'; else tmp1[2] = 'M'; } else return step; if (tmp1 != "MOO") { step--; if (tmp1[0] == 'M') tmp1[0] = 'O'; else tmp1[0] = 'M'; } else return step; if (tmp1 != "MOO") return-1; return step; }
intchecker(string x){ if (x.size() <= 3) returncheck(x); int mn = 10000; string a[] = {"OOO", "OOM", "OMO", "OMM", "MOO", "MOM", "MMO", "MMM"}; for (int i = 0; i < 8; i++) { if (checkSubString(a[i], x)) { int tmp = check(a[i]); if (tmp != -1) mn = min(mn, (tmp + x.size() - 3)); } } if (mn == 10000) return-1; return mn; }
intmain(){ scanf("%d", &n); for (int i = 0; i < n; i++) { cin >> str; printf("%d\n", checker(str)); str = ""; } return0; }