結果

問題 No.3587 Too Good to Swap
コンテスト
ユーザー marc2825
提出日時 2026-05-26 11:35:44
言語 Python3
(3.14.3 + numpy 2.4.4 + scipy 1.17.1)
コンパイル:
python3 -mpy_compile _filename_
実行:
python3 _filename_
結果
AC  
実行時間 394 ms / 2,000 ms
コード長 1,539 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 296 ms
コンパイル使用メモリ 21,540 KB
実行使用メモリ 15,868 KB
最終ジャッジ日時 2026-07-10 20:52:46
合計ジャッジ時間 8,268 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge3_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 49
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

Q = int(input())

for _ in range(Q):
    S = input()
    T = input()

    cntS = [0, 0, 0]  # d, o, g
    cntT = [0, 0, 0]

    for c in S:
        if c == 'd':
            cntS[0] += 1
        elif c == 'o':
            cntS[1] += 1
        else:
            cntS[2] += 1

    for c in T:
        if c == 'd':
            cntT[0] += 1
        elif c == 'o':
            cntT[1] += 1
        else:
            cntT[2] += 1

    if cntS != cntT:
        print("No")
        continue

    wallS = False
    wallT = False

    for idx, X in enumerate([S, T]):
        hasG = False
        hasD = False
        seenD = False
        orderOK = True

        for c in X:
            if c == 'o':
                continue

            if c == 'd':
                hasD = True
                seenD = True
            else:
                hasG = True
                if seenD:
                    orderOK = False

        wall = False

        if hasG and hasD and orderOK:
            lastG = -1
            firstD = len(X)

            for i in range(len(X)):
                if X[i] == 'g':
                    lastG = i
                if X[i] == 'd':
                    firstD = i
                    break

            betweenO = 0
            for i in range(lastG + 1, firstD):
                if X[i] == 'o':
                    betweenO += 1

            wall = betweenO >= 3

        if idx == 0:
            wallS = wall
        else:
            wallT = wall

    if wallS == wallT:
        print("Yes")
    else:
        print("No")
0