結果

問題 No.252 "良問"(良問とは言っていない (2)
ユーザー koyumeishikoyumeishi
提出日時 2015-07-25 01:22:23
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 86 ms / 2,000 ms
コード長 1,111 bytes
コンパイル時間 652 ms
コンパイル使用メモリ 82,580 KB
実行使用メモリ 12,272 KB
最終ジャッジ日時 2023-09-23 04:05:10
合計ジャッジ時間 1,679 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
4,376 KB
testcase_01 AC 86 ms
4,380 KB
testcase_02 AC 43 ms
5,100 KB
testcase_03 AC 46 ms
12,068 KB
testcase_04 AC 46 ms
12,272 KB
testcase_05 AC 44 ms
12,020 KB
testcase_06 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <cstdio>
#include <sstream>
#include <map>
#include <string>
#include <algorithm>
#include <queue>
#include <cmath>
#include <set>
using namespace std;

string good = "good";
string problem = "problem";

int main(){
	int t;
	cin >> t;
	while(t--){
		string s;
		cin >> s;
		int n = s.size();

		vector<int> g(n, 1e9);
		int num_good = 0;
		for(int i=0; i+good.size()<=n; i++){
			int cnt = 0;
			for(int j=0; j<good.size(); j++){
				if(s[i+j] != good[j]) cnt++;
			}
			g[i] = cnt + num_good;
			if(cnt == 0) num_good++;
		}

		vector<int> p(n, 1e9);
		set<int> e;
		int num_pro = 0;
		for(int i=0; i+problem.size()<=n; i++){
			int cnt = 0;
			for(int j=0; j<problem.size(); j++){
				if(s[i+j] != problem[j]) cnt++;
			}
			p[i] = cnt + num_pro;
			if(cnt == 0){
				num_pro++;
				e.insert(i);
			}
		}

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

		int ans = 1e9;
		for(int i=0; i+good.size()+problem.size()<=n; i++){
			ans = min(ans, g[i] + p[i+good.size()] + (e.count(i) + e.count(i-1)?-1:0) );
		}
		cout << ans << endl;
	}
	return 0;
}
0