結果

問題 No.252 "良問"(良問とは言っていない (2)
ユーザー けーむけーむ
提出日時 2020-08-20 18:27:19
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,876 bytes
コンパイル時間 2,370 ms
コンパイル使用メモリ 175,584 KB
実行使用メモリ 44,848 KB
最終ジャッジ日時 2024-04-21 17:08:28
合計ジャッジ時間 7,919 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 239 ms
6,940 KB
testcase_02 AC 790 ms
13,500 KB
testcase_03 AC 830 ms
44,816 KB
testcase_04 AC 840 ms
44,848 KB
testcase_05 AC 836 ms
44,848 KB
testcase_06 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for(ll i = 0, i##_len = (n); i < i##_len; i++)
#define reps(i, s, n) for(ll i = (s), i##_len = (n); i < i##_len; i++)
#define rrep(i, n) for(ll i = (n) - 1; i >= 0; i--)
#define rreps(i, e, n) for(ll i = (n) - 1; i >= (e); i--)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) ((ll)(x).size())
#define len(x) ((ll)(x).length())
#define endl "\n"
template<class T> void chmax(T &a, const T b){ a = max(a, b); }
template<class T> void chmin(T &a, const T b){ a = min(a, b); }

template<class T>
struct SegmentTree {
private:
    int n;
    vector<T> node;
    T def;
    function<T(T,T)> lop;
    function<T(T,T)> out;
    
public:
    SegmentTree() {}
    SegmentTree(const vector<T> &v, T _def, function<T(T,T)> _lop, function<T(T,T)> _out) {
        int vl = (int)v.size();
        n = 1;
        while(n < vl) n *= 2;
        def = _def; lop = _lop; out = _out;
        node = vector<T>(2 * n - 1, def);
        for(int i = 0; i < vl; i++) node[i + n - 1] = v[i];
        for(int i = n - 2; i >= 0; i--) node[i] = out(node[i * 2 + 1], node[i * 2 + 2]);
    }
    
    void update(int idx, T val) {
        idx += (n - 1);
        node[idx] = lop(node[idx], val);
        while(idx > 0) {
            idx = (idx - 1) / 2;
            node[idx] = out(node[idx * 2 + 1], node[idx * 2 + 2]);
        }
    }
    
    T query(int a, int b, int k = 0, int l = 0, int r = -1) {
        if (r < 0) r = n;
        if ((r <= a) || (b <= l)) return def;
        if ((a <= l) && (r <= b)) return node[k];
        return out(query(a, b, 2 * k + 1, l, (l + r) / 2), query(a, b, 2 * k + 2, (l + r) / 2, r));
    }
};

void solve() {
    const ll inf = LONG_LONG_MAX / 2 - 1;
    const string gs = "good";
    const string ps = "problem";
    string s;
    cin >> s;
    ll n = len(s);
    auto lop = [](ll a, ll b){ return b; };
    auto out = [](ll a, ll b){ return min(a, b); };
    SegmentTree<ll> gst(vector<ll>(n, inf), inf, lop, out);
    SegmentTree<ll> pst(vector<ll>(n, inf), inf, lop, out);
    rep(i, n - len(gs) + 1) {
        ll cnt = 0;
        rep(j, len(gs)) {
            if (s[i + j] != gs[j]) cnt++;
        }
        gst.update(i, cnt);
    }
    rep(i, n - len(ps) + 1) {
        ll cnt = 0;
        rep(j, len(ps)) {
            if (s[i + j] != ps[j]) cnt++;
        }
        pst.update(i, cnt);
    }
    ll ans = inf;
    rep(i, n - len(gs) + 1) {
        ll tmp = 0;
        tmp += gst.query(i, i + 1);
        tmp += pst.query(i + len(gs), n);
        if (pst.query(0, i) == 0) tmp++;
        chmin(ans, tmp);
    }
    cout << ans << endl;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    // ifstream in("input.txt");
    // cin.rdbuf(in.rdbuf());
    ll t;
    cin >> t;
    rep(i, t) solve();
    return 0;
}
0