Q05
unknown
python
2 years ago
933 B
9
Indexable
s1 = input()
s2 = input()
costs = list(map(int, input().split()))
# ایجاد یک ماتریس با ابعاد (len(s1)+1)×(len(s2)+1) برای ذخیره کردن هزینههای تغییر دنباله
dp = [[0 for j in range(len(s2)+1)] for i in range(len(s1)+1)]
# پر کردن مقادیر اولیه ماتریس
for i in range(1, len(s1)+1):
dp[i][0] = dp[i-1][0] + costs[ord(s1[i-1])-ord('A')]
for j in range(1, len(s2)+1):
dp[0][j] = dp[0][j-1] + costs[ord(s2[j-1])-ord('A')]
# بهروزرسانی مقادیر ماتریس
for i in range(1, len(s1)+1):
for j in range(1, len(s2)+1):
if s1[i-1] == s2[j-1]:
dp[i][j] = dp[i-1][j-1]
else:
dp[i][j] = min(dp[i-1][j]+costs[ord(s1[i-1])-ord('A')], dp[i][j-1]+costs[ord(s2[j-1])-ord('A')], dp[i-1][j-1]+costs[ord(s1[i-1])-ord('A')]+costs[ord(s2[j-1])-ord('A')])
print(dp[len(s1)][len(s2)])Editor is loading...