結果

問題 No.252 "良問"(良問とは言っていない (2)
ユーザー codershifthcodershifth
提出日時 2016-05-05 16:12:31
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 78 ms / 2,000 ms
コード長 2,429 bytes
コンパイル時間 2,571 ms
コンパイル使用メモリ 147,372 KB
実行使用メモリ 15,884 KB
最終ジャッジ日時 2023-07-28 03:20:50
合計ジャッジ時間 2,592 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 37 ms
4,376 KB
testcase_01 AC 78 ms
4,376 KB
testcase_02 AC 42 ms
6,248 KB
testcase_03 AC 42 ms
15,884 KB
testcase_04 AC 43 ms
15,780 KB
testcase_05 AC 42 ms
15,672 KB
testcase_06 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

typedef long long ll;
typedef unsigned long long ull;

#define FOR(i,a,b) for(int (i)=(a);i<(b);i++)
#define REP(i,n) FOR(i,0,n)
#define RANGE(vec) (vec).begin(),(vec).end()

using namespace std;


class GoodProblem2
{
public:
    void solve(void)
    {
        int T;
        cin>>T;

        while (T--)
        {
            string S;
            cin>>S;

            const int inf = (1<<30);
            int N = S.length();
            // S[i] からスタートしたとき problem,good にするために必要な置換数
            vector<int> probCost(N,inf);
            vector<int> goodCost(N,inf);

            const string good = "good";
            const string prob = "problem";


            // O(N*10)
            for (int i = 0; i+6 < N; ++i)
            {
                probCost[i] = 0;
                REP(j,7)
                {
                    if ( S[i+j] != prob[j] )
                        ++probCost[i];
                }
            }
            for (int i = 0; i+3 < N; ++i)
            {
                goodCost[i] = 0;
                REP(j,4)
                {
                    if ( S[i+j] != good[j] )
                        ++goodCost[i];
                }
            }

            // S[i] 以降で problem を作るときの最小コストを計算
            for (int i = N-8; i >= 0; --i)
                probCost[i] = min(probCost[i], probCost[i+1]);

            // 最初に見つかる good が problem の後ろにある場合の追加コスト
            // S[i] 以前に problem が出現している回数を累積しておけばよい
            vector<int> addCost(N,0);
            for (int i = 6; i < N; ++i)
            {
                int j;
                addCost[i] = addCost[i-1];// 累積
                for (j = 0; j < 7; ++j)
                {
                    if ( S[(i-6)+j] != prob[j] )
                        break;
                }
                // match
                if ( j == 7 )
                    ++addCost[i];
            }
            int ret = inf;
            for (int i = 0; i+6 < N; ++i)
                ret = min(ret, goodCost[i] + addCost[i] + probCost[i+4]);

            cout<<ret<<endl;
        }
    }
};

#if 1
int main(int argc, char *argv[])
{
        ios::sync_with_stdio(false);
        auto obj = new GoodProblem2();
        obj->solve();
        delete obj;
        return 0;
}
#endif
0