結果

問題 No.252 "良問"(良問とは言っていない (2)
ユーザー krotonkroton
提出日時 2015-07-22 11:33:52
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 89 ms / 2,000 ms
コード長 1,328 bytes
コンパイル時間 1,189 ms
コンパイル使用メモリ 148,360 KB
実行使用メモリ 19,924 KB
最終ジャッジ日時 2023-09-22 21:11:28
合計ジャッジ時間 2,306 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
4,380 KB
testcase_01 AC 89 ms
4,380 KB
testcase_02 AC 48 ms
6,676 KB
testcase_03 AC 48 ms
19,924 KB
testcase_04 AC 47 ms
19,836 KB
testcase_05 AC 47 ms
19,832 KB
testcase_06 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

const int INF = 1 << 20;
const string good = "good";
const string problem = "problem";

vector<int> countDiff(const string& S, const string& target){
	const int n = S.size();
	const int m = target.size();
	vector<int> res(n, INF);
	for(int i=0;i<=n-m;i++){
		int diff = 0;
		for(int j=0;j<m;j++){
			if(S[i+j] != target[j]){
				++diff;
			}
		}
		res[i] = diff;
	}
	return res;
}

int solve(const string& S){
	const int n = S.size();
	const auto goodDiff = countDiff(S, good);
	const auto problemDiff = countDiff(S, problem);

	auto afterProblem = problemDiff;
	for(int i=n-2;i>=0;i--){
		afterProblem[i] = min(afterProblem[i+1], afterProblem[i]);
	}

	vector<int> mustDelete(n, 0);
	for(int i=0;i<n;i++){
		if(goodDiff[i] == 0){
			++mustDelete[i+good.size()-1];
		}
		if(problemDiff[i] == 0){
			++mustDelete[i+problem.size()-1];
		}
	}
	for(int i=1;i<n;i++){
		mustDelete[i] += mustDelete[i-1];
	}

	int res = INF;
	for(int i=0;i<n;i++){
		if(i + good.size() + problem.size() > n){
			break;
		}
		int cost = goodDiff[i];
		if(i - 1 >= 0){
			cost += mustDelete[i-1];
		}
		cost += afterProblem[i+good.size()];
		res = min(res, cost);
	}
	return res;
}

int main(){
	int T;
	cin >> T;
    
	while(T--){
		string S;
		cin >> S;
		cout << solve(S) << endl;	
	}
    
	return 0;
}
0