#include #include #include #include #include #include using namespace std; const string good = "good"; const string problem = "problem"; const int INF = 1e8; int main() { int t; string s; int len_g = good.size(); int len_p = problem.size(); cin >> t; vector pos_g; vector pos_p; vector is_p; int diff; vector answers; while (t--) { cin >> s; int limit = s.size() - len_p + 1; pos_g.assign(len_g + 1, INF); pos_p.assign(len_p + 1, -1); is_p.assign(s.size(), false); for (int i = 0; i < limit; i++) { diff = 0; for (int j = 0; j < len_g; j++) { if (s[i + j] != good[j]) { diff++; } } pos_g[diff] = min(pos_g[diff], i); diff = 0; for (int j = 0; j < len_p; j++) { if (s[i + j] != problem[j]) { diff++; } } pos_p[diff] = max(pos_p[diff], i); if (diff == 0) { is_p[i] = true; } } int ans = len_g + len_p; int add; // goodの前方にあるproblemを壊す for (int i = 0; i <= len_g; i++) { for (int j = 0; j <= len_p; j++) { if (pos_g[i] + len_g - 1 < pos_p[j]) { if (pos_g[i] < len_p) { add = 0; } else { add = count(is_p.begin(), is_p.begin() + pos_g[i] - len_p + 1, true); } ans = min(ans, i + j + add); } } } answers.push_back(ans); } for (int i = 0; i < answers.size(); i++) { cout << answers[i] << endl; } return 0; }