結果

問題 No.197 手品
ユーザー lam6er
提出日時 2025-03-20 20:38:10
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,089 bytes
コンパイル時間 154 ms
コンパイル使用メモリ 82,828 KB
実行使用メモリ 56,164 KB
最終ジャッジ日時 2025-03-20 20:38:23
合計ジャッジ時間 3,116 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 20 WA * 23
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict

def compute_parity(perm):
    n = len(perm)
    visited = [False] * n
    parity = 0
    for i in range(n):
        if not visited[i]:
            cycle_length = 0
            j = i
            while not visited[j]:
                visited[j] = True
                j = perm[j]
                cycle_length += 1
            parity += cycle_length - 1
    return parity % 2

def solve():
    S = input().strip()
    N = int(input())
    T = input().strip()
    
    if sorted(S) != sorted(T):
        print("SUCCESS")
        return
    
    # Build permutation by tracking positions of each character in S
    pos_map = defaultdict(list)
    for idx, char in enumerate(S):
        pos_map[char].append(idx)
    
    current_ptr = defaultdict(int)
    permutation = []
    for char in T:
        available = pos_map[char]
        permutation.append(available[current_ptr[char]])
        current_ptr[char] += 1
    
    parity = compute_parity(permutation)
    
    if parity == N % 2:
        print("FAILURE")
    else:
        print("SUCCESS")

solve()
0