結果

問題 No.252 "良問"(良問とは言っていない (2)
ユーザー tnakao0123tnakao0123
提出日時 2016-03-30 23:40:23
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 83 ms / 2,000 ms
コード長 1,682 bytes
コンパイル時間 778 ms
コンパイル使用メモリ 86,220 KB
実行使用メモリ 16,360 KB
最終ジャッジ日時 2024-04-10 06:38:04
合計ジャッジ時間 2,087 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 37 ms
7,632 KB
testcase_01 AC 83 ms
7,752 KB
testcase_02 AC 37 ms
12,628 KB
testcase_03 AC 39 ms
16,224 KB
testcase_04 AC 39 ms
16,228 KB
testcase_05 AC 38 ms
16,360 KB
testcase_06 AC 2 ms
7,756 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 252.cc: No.252 "良問"(良問とは言っていない (2) - yukicoder
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int MAX_N = 1000000;
const int INF = 1 << 30;

const string STR_G("good");
const string STR_P("problem");

/* typedef */

/* global variables */

int egs[MAX_N + 1], eps[MAX_N + 1], pcs[MAX_N + 1];

/* subroutines */

void make_es(const string &s, const string &t, int es[]) {
  int sn = s.size(), tn = t.size();

  for (int i = 0; i <= sn - tn; i++) {
    int cnt = 0;
    for (int j = 0; j < tn; j++)
      if (s[i + j] != t[j]) cnt++;
    es[i] = cnt;
  }
}

/* main */

int main() {
  int tn;
  cin >> tn;

  while (tn--) {
    string s;
    cin >> s;

    make_es(s, STR_G, egs);
    make_es(s, STR_P, eps);

    int sn = s.size(), egn = sn - 4 + 1, epn = sn - 7 + 1;

    if (false) {
      cout << s << endl;
      for (int i = 0; i < egn; i++) printf("%d ", egs[i]); putchar('\n');
      for (int i = 0; i < epn; i++) printf("%d ", eps[i]); putchar('\n');
    }

    pcs[0] = 0;
    for (int i = 0; i < epn; i++)
      pcs[i + 1] = pcs[i] + (eps[i] == 0 ? 1 : 0);

    int mind = INF, minp = INF;
    for (int i = epn - 1; i >= 4; i--) {
      if (minp > eps[i]) minp = eps[i];
      int d = pcs[i - 4] + egs[i - 4] + minp;
      if (mind > d) mind = d;
    }

    printf("%d\n", mind);
  }
  return 0;
}
0