結果

問題 No.150 "良問"(良問とは言っていない
ユーザー otsnskotsnsk
提出日時 2015-02-17 13:10:39
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 3 ms / 5,000 ms
コード長 2,129 bytes
コンパイル時間 824 ms
コンパイル使用メモリ 73,988 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-19 09:23:44
合計ジャッジ時間 1,536 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 1 ms
6,940 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 1 ms
6,944 KB
testcase_04 AC 1 ms
6,940 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 3 ms
6,940 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 2 ms
6,940 KB
testcase_10 AC 2 ms
6,944 KB
testcase_11 AC 2 ms
6,944 KB
testcase_12 AC 2 ms
6,940 KB
testcase_13 AC 2 ms
6,940 KB
testcase_14 AC 2 ms
6,944 KB
testcase_15 AC 2 ms
6,940 KB
testcase_16 AC 2 ms
6,940 KB
testcase_17 AC 1 ms
6,944 KB
testcase_18 AC 3 ms
6,940 KB
testcase_19 AC 2 ms
6,940 KB
testcase_20 AC 2 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/*

http://yukicoder.me/problems/344
No. 150

 */
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>

#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<class T>
struct SegmentTree {
    int size;
    vector<T> 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<int> 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;
}
0