結果

問題 No.3587 Too Good to Swap
コンテスト
ユーザー marc2825
提出日時 2026-06-13 16:59:56
言語 PyPy3
(7.3.17)
コンパイル:
pypy3 -mpy_compile _filename_
実行:
pypy3 _filename_
結果
WA  
実行時間 -
コード長 1,476 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 238 ms
コンパイル使用メモリ 96,236 KB
実行使用メモリ 96,680 KB
最終ジャッジ日時 2026-07-10 21:02:27
合計ジャッジ時間 8,119 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample WA * 1
other AC * 17 WA * 22 TLE * 1 -- * 9
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

import sys
import bisect

def solve():
    input = sys.stdin.read
    data = input().split()
    if not data:
        return
    
    Q = int(data[0])
    idx = 1
    
    out = []
    for _ in range(Q):
        S = data[idx]
        T = data[idx+1]
        idx += 2
        
        if sorted(S) != sorted(T):
            out.append("No")
            continue
            
        s_g = [i for i, c in enumerate(S) if c == 'g']
        s_d = [i for i, c in enumerate(S) if c == 'd']
        s_o = [i for i, c in enumerate(S) if c == 'o']
        
        t_g = [i for i, c in enumerate(T) if c == 'g']
        t_d = [i for i, c in enumerate(T) if c == 'd']
        
        possible = True
        
        for i, g_pos in enumerate(s_g):
            g_dest = t_g[i]
            
            for j, d_pos in enumerate(s_d):
                d_dest = t_d[j]
                
                if g_pos < d_pos:
                    l = bisect.bisect_right(s_o, g_pos)
                    r = bisect.bisect_left(s_o, d_pos)
                    o_count = r - l
                    
                    if o_count >= 2:
                        if g_dest > d_dest:
                            possible = False
                            break
            if not possible:
                break
                
        if possible:
            out.append("Yes")
        else:
            out.append("No")
            
    print('\n'.join(out))

if __name__ == '__main__':
    solve()
0