結果
| 問題 | No.3587 Too Good to Swap |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2026-05-26 19:43:55 |
| 言語 | C++17 (gcc 15.2.0 + boost 1.90.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 2,397 bytes |
| 記録 | |
| コンパイル時間 | 1,154 ms |
| コンパイル使用メモリ | 212,244 KB |
| 実行使用メモリ | 9,420 KB |
| 最終ジャッジ日時 | 2026-07-10 20:53:37 |
| 合計ジャッジ時間 | 3,254 ms |
|
ジャッジサーバーID (参考情報) |
judge1_0 / judge2_0 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | WA * 1 |
| other | WA * 49 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
// 変化位置の近傍だけを見て、局所的に危険そうなら true
bool locally_dangerous(const string& s, int l, int r) {
int n = (int)s.size();
// 変化した範囲の少し周辺だけ見る
int L = max(0, l - 3);
int R = min(n - 1, r + 3);
for (int i = L; i <= R; i++) {
// g の左に d がある場合だけ危険視
// ここでは「すぐ左」だけ見る
if (s[i] == 'g') {
if (i - 1 >= 0 && s[i - 1] == 'd') {
return true;
}
}
// d の右に g がある場合だけ危険視
// ここでは「すぐ右」だけ見る
if (s[i] == 'd') {
if (i + 1 < n && s[i + 1] == 'g') {
return true;
}
}
}
return false;
}
// good -> dog
bool apply_good_to_dog(string& s) {
int n = (int)s.size();
for (int i = 0; i + 4 <= n; i++) {
if (s.substr(i, 4) != "good") continue;
string t = s;
t.replace(i, 4, "dog");
// good[4] -> dog[3] なので、変化後の範囲は i..i+2 あたり
if (locally_dangerous(t, i, i + 2)) {
continue;
}
s = t;
return true;
}
return false;
}
// dog -> good
bool apply_dog_to_good(string& s) {
int n = (int)s.size();
for (int i = 0; i + 3 <= n; i++) {
if (s.substr(i, 3) != "dog") continue;
string t = s;
t.replace(i, 3, "good");
// dog[3] -> good[4] なので、変化後の範囲は i..i+3 あたり
if (locally_dangerous(t, i, i + 3)) {
continue;
}
s = t;
return true;
}
return false;
}
// 嘘貪欲:
// 左から見て、局所的に危険でなければ操作する。
// good -> dog を優先し、無理なら dog -> good を試す。
string fake_solve(string s, int limit = 100000) {
for (int step = 0; step < limit; step++) {
bool changed = false;
if (apply_good_to_dog(s)) {
changed = true;
} else if (apply_dog_to_good(s)) {
changed = true;
}
if (!changed) break;
}
return s;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
string S;
cin >> S;
string ans = fake_solve(S);
cout << ans << '\n';
return 0;
}