結果

問題 No.252 "良問"(良問とは言っていない (2)
ユーザー parukiparuki
提出日時 2016-07-27 14:40:50
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 76 ms / 2,000 ms
コード長 1,929 bytes
コンパイル時間 1,571 ms
コンパイル使用メモリ 167,668 KB
実行使用メモリ 13,036 KB
最終ジャッジ日時 2023-08-06 14:52:20
合計ジャッジ時間 2,850 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 34 ms
4,380 KB
testcase_01 AC 76 ms
4,376 KB
testcase_02 AC 38 ms
5,132 KB
testcase_03 AC 39 ms
13,036 KB
testcase_04 AC 38 ms
12,724 KB
testcase_05 AC 39 ms
12,856 KB
testcase_06 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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)<<endl
#define smax(x,y) (x)=max((x),(y))
#define smin(x,y) (x)=min((x),(y))
#define MEM(x,y) memset((x),(y),sizeof (x))
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<ll> 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);
    }
}
0