結果

問題 No.422 文字列変更 (Hard)
ユーザー lam6er
提出日時 2025-03-26 15:55:30
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 612 ms / 3,000 ms
コード長 3,648 bytes
コンパイル時間 401 ms
コンパイル使用メモリ 82,416 KB
実行使用メモリ 145,764 KB
最終ジャッジ日時 2025-03-26 15:56:27
合計ジャッジ時間 8,925 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 16
権限があれば一括ダウンロードができます

ソースコード

diff #
プレゼンテーションモードにする

n, m = map(int, input().split())
S = input().strip()
T = input().strip()
INF = float('inf')
# Initialize DP tables
M = [[INF] * (m + 1) for _ in range(n + 1)]
I = [[INF] * (m + 1) for _ in range(n + 1)]
D = [[INF] * (m + 1) for _ in range(n + 1)]
M[0][0] = 0 # Base case: no characters processed
for i in range(n + 1):
for j in range(m + 1):
if i == 0 and j == 0:
continue
# Update M[i][j] (match or substitution)
if i > 0 and j > 0:
cost = 0 if S[i-1] == T[j-1] else 5
prev_min = min(M[i-1][j-1], I[i-1][j-1], D[i-1][j-1])
if prev_min + cost < M[i][j]:
M[i][j] = prev_min + cost
# Update I[i][j] (insertion)
if j > 0:
candidates = []
if I[i][j-1] != INF:
candidates.append(I[i][j-1] + 2)
if M[i][j-1] != INF:
candidates.append(M[i][j-1] + 9)
if D[i][j-1] != INF:
candidates.append(D[i][j-1] + 9)
if candidates:
min_val = min(candidates)
if min_val < I[i][j]:
I[i][j] = min_val
# Update D[i][j] (deletion)
if i > 0:
candidates = []
if D[i-1][j] != INF:
candidates.append(D[i-1][j] + 2)
if M[i-1][j] != INF:
candidates.append(M[i-1][j] + 9)
if I[i-1][j] != INF:
candidates.append(I[i-1][j] + 9)
if candidates:
min_val = min(candidates)
if min_val < D[i][j]:
D[i][j] = min_val
# Determine the minimal cost
min_cost = min(M[n][m], I[n][m], D[n][m])
# Backtrack to find the alignment
s_aligned = []
t_aligned = []
current_state = None
if min_cost == M[n][m]:
current_state = 'M'
elif min_cost == I[n][m]:
current_state = 'I'
else:
current_state = 'D'
i, j = n, m
while i > 0 or j > 0:
if current_state == 'M':
s_aligned.append(S[i-1])
t_aligned.append(T[j-1])
cost = 0 if S[i-1] == T[j-1] else 5
prev_val = M[i][j] - cost
if i > 0 and j > 0 and M[i-1][j-1] == prev_val:
i -= 1
j -= 1
current_state = 'M'
elif i > 0 and j > 0 and I[i-1][j-1] == prev_val:
i -= 1
j -= 1
current_state = 'I'
elif i > 0 and j > 0 and D[i-1][j-1] == prev_val:
i -= 1
j -= 1
current_state = 'D'
elif current_state == 'I':
s_aligned.append('-')
t_aligned.append(T[j-1])
current_val = I[i][j]
if j > 0 and I[i][j-1] != INF and I[i][j-1] + 2 == current_val:
j -= 1
current_state = 'I'
elif j > 0 and M[i][j-1] != INF and M[i][j-1] + 9 == current_val:
j -= 1
current_state = 'M'
elif j > 0 and D[i][j-1] != INF and D[i][j-1] + 9 == current_val:
j -= 1
current_state = 'D'
elif current_state == 'D':
s_aligned.append(S[i-1])
t_aligned.append('-')
current_val = D[i][j]
if i > 0 and D[i-1][j] != INF and D[i-1][j] + 2 == current_val:
i -= 1
current_state = 'D'
elif i > 0 and M[i-1][j] != INF and M[i-1][j] + 9 == current_val:
i -= 1
current_state = 'M'
elif i > 0 and I[i-1][j] != INF and I[i-1][j] + 9 == current_val:
i -= 1
current_state = 'I'
s_aligned = s_aligned[::-1]
t_aligned = t_aligned[::-1]
print(min_cost)
print(''.join(s_aligned))
print(''.join(t_aligned))
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0