#include "bits/stdc++.h" using namespace std; #define FOR(i,j,k) for(int (i)=(j);(i)<(int)(k);++(i)) #define rep(i,j) FOR(i,0,j) #define each(x,y) for(auto &(x):(y)) #define mp make_pair #define all(x) (x).begin(),(x).end() #define debug(x) cout<<#x<<": "<<(x)< pii; typedef vector vi; typedef vector vll; static const int MAX_LEN = 1000001; string nstr(){ static char res_[MAX_LEN]; scanf("%s",res_); return string(res_); } const int INF = INT_MAX; const string G = "good", P = "problem"; void solve(string &S){ const int N = sz(S); // p[i]: インデックスi以降を始点として、最小コストで作れる"problem"のコスト // sp[i]: インデックスiから"problem"がスタートしている。 vi p(N + 1, INF), sp(N + 1, 0); for(int i = 0; i + 7 <= N; ++i){ int x = 0; rep(j, 7)if(S[i + j] != P[j])++x; p[i] = x; if(x == 0)sp[i] = 1; } for(int i = N - 1; i >= 0; i--){ smin(p[i], p[i + 1]); } int bad = 0, ans = INF; // i は"good"の開始位置 for(int i = 0; i + 11 <= N; ++i){ // "good"の開始位置より手前で"problem"が出現してしまっているので1箇所変更して打ち消す。 if(i - 7 >= 0)bad += sp[i - 7]; // x = bad + p[i+4] + cnt(S[i+j]!=G[j]) // bad: "good"より手前に現れた"problem" // p[i+4]: 後ろにつける"problem"の最小コスト // cnt(S[i+j]!=G[j]): "good"のコスト int x = bad + p[i + 4]; rep(j, 4)if(S[i + j] != G[j])++x; smin(ans, x); } cout << ans << endl; } int main(){ int T; cin >> T; while(T--){ string S = nstr(); solve(S); } }