結果

問題 No.2018 X-Y-X
ユーザー gew1fw
提出日時 2025-06-12 21:39:16
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,738 bytes
コンパイル時間 155 ms
コンパイル使用メモリ 81,604 KB
実行使用メモリ 99,060 KB
最終ジャッジ日時 2025-06-12 21:43:39
合計ジャッジ時間 3,186 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 9 WA * 22
権限があれば一括ダウンロードができます

ソースコード

diff #

def min_operations(N, S, T):
    S_list = list(S)
    T_list = list(T)
    operations = 0
    
    for j in range(N):
        if S_list[j] == T_list[j]:
            continue
        # Check if j is at the first or last position, can't have i=j-1
        if j == 0 or j == N-1:
            return -1
        # Check if the condition is satisfied
        if S_list[j-1] != S_list[j+1]:
            # Try to flip j+1 if possible
            if j+1 == N-1:
                return -1
            # Check if j+1-1 and j+1+1 are same
            if S_list[j] != S_list[j+2]:
                return -1
            # Flip j+1
            S_list[j+1] = 'A' if S_list[j+1] == 'B' else 'B'
            operations += 1
            # Now check again if condition is satisfied
            if S_list[j-1] != S_list[j+1]:
                return -1
        # Flip j
        S_list[j] = 'A' if S_list[j] == 'B' else 'B'
        operations += 1
    return operations

# 读取输入
N = int(input())
S = input().strip()
T = input().strip()

# 处理特殊情况:如果长度不同,直接返回-1(虽然题目保证长度相同)
if len(S) != len(T):
    print(-1)
else:
    # 转换为列表便于处理
    S_list = list(S)
    T_list = list(T)
    # 检查是否可以转换
    ok = True
    for i in range(N):
        if S_list[i] != T_list[i]:
            # 检查是否存在满足条件的位置来处理差异点
            if i == 0 or i == N-1:
                ok = False
                break
            if (i == 1 or i == N-2):
                pass
            else:
                pass
    if not ok:
        print(-1)
    else:
        # 计算操作次数
        res = min_operations(N, S, T)
        print(res if res != -1 else -1)
0