結果
| 問題 | No.252 "良問"(良問とは言っていない (2) |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2016-07-27 14:40:50 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.89.0) |
| 結果 |
AC
|
| 実行時間 | 72 ms / 2,000 ms |
| コード長 | 1,929 bytes |
| 記録 | |
| コンパイル時間 | 1,629 ms |
| コンパイル使用メモリ | 168,988 KB |
| 実行使用メモリ | 13,056 KB |
| 最終ジャッジ日時 | 2024-11-06 17:57:11 |
| 合計ジャッジ時間 | 2,480 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 7 |
ソースコード
#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);
}
}