結果
| 問題 |
No.252 "良問"(良問とは言っていない (2)
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2015-07-22 11:37:06 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 91 ms / 2,000 ms |
| コード長 | 1,332 bytes |
| コンパイル時間 | 1,385 ms |
| コンパイル使用メモリ | 162,164 KB |
| 実行使用メモリ | 19,984 KB |
| 最終ジャッジ日時 | 2024-07-08 11:46:36 |
| 合計ジャッジ時間 | 2,349 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 7 |
ソースコード
#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;
}