結果
| 問題 | No.3587 Too Good to Swap |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2026-05-26 11:40:34 |
| 言語 | C++17 (gcc 15.2.0 + boost 1.90.0) |
| 結果 |
AC
|
| 実行時間 | 323 ms / 2,000 ms |
| コード長 | 2,862 bytes |
| 記録 | |
| コンパイル時間 | 1,336 ms |
| コンパイル使用メモリ | 226,400 KB |
| 実行使用メモリ | 5,888 KB |
| 最終ジャッジ日時 | 2026-07-10 20:52:57 |
| 合計ジャッジ時間 | 4,004 ms |
|
ジャッジサーバーID (参考情報) |
judge3_0 / judge1_0 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 49 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
bool is_good_string(const string& s) {
return s.find("good") != string::npos;
}
bool wall_type(const string& X) {
bool hasG = false, hasD = false;
bool seenD = false;
bool orderOK = true;
// Remove all 'o'. The remaining string must be g...gd...d.
for (char c : X) {
if (c == 'o') continue;
if (c == 'd') {
hasD = true;
seenD = true;
} else { // c == 'g'
hasG = true;
if (seenD) orderOK = false;
}
}
if (!hasG || !hasD || !orderOK) return false;
int lastG = -1;
int firstD = (int)X.size();
for (int i = 0; i < (int)X.size(); i++) {
if (X[i] == 'g') lastG = i;
if (X[i] == 'd') {
firstD = i;
break;
}
}
int betweenO = 0;
for (int i = lastG + 1; i < firstD; i++) {
if (X[i] == 'o') betweenO++;
}
return betweenO >= 3;
}
bool fast_judge(const string& S, const string& T) {
vector<int> cntS(3, 0), cntT(3, 0); // d, o, g
for (char c : S) {
if (c == 'd') cntS[0]++;
else if (c == 'o') cntS[1]++;
else if (c == 'g') cntS[2]++;
}
for (char c : T) {
if (c == 'd') cntT[0]++;
else if (c == 'o') cntT[1]++;
else if (c == 'g') cntT[2]++;
}
if (cntS != cntT) return false;
return wall_type(S) == wall_type(T);
}
bool bfs_judge(const string& S, const string& T) {
if (S.size() != T.size()) return false;
if (is_good_string(S) || is_good_string(T)) return false;
queue<string> que;
set<string> seen;
que.push(S);
seen.insert(S);
while (!que.empty()) {
string cur = que.front();
que.pop();
if (cur == T) return true;
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_string(nxt)) continue;
if (seen.count(nxt)) continue;
seen.insert(nxt);
que.push(nxt);
}
}
return false;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int Q;
cin >> Q;
const int BFS_LIMIT = 8;
while (Q--) {
string S, T;
cin >> S >> T;
bool ans = fast_judge(S, T);
if ((int)S.size() <= BFS_LIMIT) {
bool brute = bfs_judge(S, T);
if (ans != brute) {
cerr << "Mismatch found!\n";
cerr << "S = " << S << '\n';
cerr << "T = " << T << '\n';
cerr << "fast = " << (ans ? "Yes" : "No") << '\n';
cerr << "brute = " << (brute ? "Yes" : "No") << '\n';
return 1;
}
}
cout << (ans ? "Yes" : "No") << '\n';
}
return 0;
}