/* http://yukicoder.me/problems/344 No. 150 */ #include #include #include #include #define FORR(i,b,e) for(int i=(b);i<(int)(e);++i) #define FOR(i,e) FORR(i,0,e) #define sortuniq(v) sort(v.begin(), v.end()), v.erase(unique(v.begin(),v.end()),v.end()) #define dump(var) cerr << #var ": " << var << "\n" #define dumpc(con) for(auto& e: con) cerr << e << " "; cerr<<"\n" typedef long long ll; typedef unsigned long long ull; const double EPS = 1e-6; const int d4[] = {0, -1, 0, 1, 0}; using namespace std; const int INF = 1e9; template struct SegmentTree { int size; vector v; SegmentTree(int req_size) { size = 1<<1; while (size < req_size) { size <<= 1; } v.resize(size << 1); } void update(int p, T val) { p += size-1; v[p] = val; while (p) { p >>= 1; v[p] = min(v[p<<1], v[(p<<1)+1]); } } T minVal(int a, int b, int k, int l, int r) { if (r <= a || b <= l) return INF; if (a <= l && r <= b) return v[k]; return min(minVal(a, b, k<<1, l, (l+r)>>1), minVal(a, b, (k<<1)+1, (l+r)>>1, r)); } T minVal(int a, int b) { return minVal(a, b, 1, 1, size+1); } }; inline int edit_score(const string &src, int pos, const string &t) { int score = 0; FOR(i, t.size()) { score += src[pos+i] != t[i]; } return score; } int main() { cin.tie(0); ios::sync_with_stdio(false); const string sg = "good", sp = "problem"; int T; cin >> T; while (T--) { string S; cin >> S; SegmentTree st(S.size()); FORR(i, sg.size(), S.size()-sp.size()+1) { st.update(i, edit_score(S, i, sp)); } int min_score = INF; FOR(i, S.size()-sg.size()-sp.size()+1) { min_score = min(min_score, edit_score(S, i, sg) + st.minVal(i+sg.size(), S.size()-sp.size()+1)); } cout << min_score << endl; } return 0; }