結果
| 問題 | No.3587 Too Good to Swap |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2026-05-30 09:18:22 |
| 言語 | C++17 (gcc 15.2.0 + boost 1.90.0) |
| 結果 |
AC
|
| 実行時間 | 219 ms / 2,000 ms |
| コード長 | 2,598 bytes |
| 記録 | |
| コンパイル時間 | 1,378 ms |
| コンパイル使用メモリ | 226,332 KB |
| 実行使用メモリ | 5,888 KB |
| 最終ジャッジ日時 | 2026-07-10 20:57:38 |
| 合計ジャッジ時間 | 3,770 ms |
|
ジャッジサーバーID (参考情報) |
judge1_1 / judge3_0 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 49 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
bool is_good(const string& s) {
return s.find("good") != string::npos;
}
bool is_special(const string& s) {
int n = (int)s.size();
int last_g = -1;
int first_d = -1;
for (int i = 0; i < n; i++) {
if (s[i] == 'g') last_g = i;
if (s[i] == 'd' && first_d == -1) first_d = i;
}
if (last_g == -1 || first_d == -1) return false;
return last_g < first_d && first_d - last_g - 1 >= 3;
}
bool solve_main(const string& S, const string& T) {
int cdS = 0, coS = 0, cgS = 0;
int cdT = 0, coT = 0, cgT = 0;
for (char c : S) {
if (c == 'd') cdS++;
if (c == 'o') coS++;
if (c == 'g') cgS++;
}
for (char c : T) {
if (c == 'd') cdT++;
if (c == 'o') coT++;
if (c == 'g') cgT++;
}
if (cdS != cdT) return false;
if (coS != coT) return false;
if (cgS != cgT) return false;
return is_special(S) == is_special(T);
}
bool solve_bruteforce(const string& S, const string& T) {
if (S == T) return true;
queue<string> q;
unordered_set<string> visited;
q.push(S);
visited.insert(S);
while (!q.empty()) {
string cur = q.front();
q.pop();
int n = (int)cur.size();
for (int i = 0; i + 1 < n; i++) {
string nxt = cur;
swap(nxt[i], nxt[i + 1]);
if (is_good(nxt)) continue;
if (visited.count(nxt)) continue;
if (nxt == T) return true;
visited.insert(nxt);
q.push(nxt);
}
}
return false;
}
bool validate_input_string(const string& s) {
for (char c : s) {
if (c != 'd' && c != 'o' && c != 'g') {
return false;
}
}
return !is_good(s);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int Q;
cin >> Q;
assert(1 <= Q && Q <= 200000);
long long total_len = 0;
while (Q--) {
string S, T;
cin >> S >> T;
assert(!S.empty());
assert(S.size() == T.size());
total_len += (long long)S.size();
assert(validate_input_string(S));
assert(validate_input_string(T));
bool ans;
if ((int)S.size() <= 8) {
bool brute = solve_bruteforce(S, T);
bool main_ans = solve_main(S, T);
assert(brute == main_ans);
ans = brute;
} else {
ans = solve_main(S, T);
}
cout << (ans ? "Yes" : "No") << '\n';
}
assert(total_len <= 200000);
return 0;
}