ShaysXIA/CodeInsightBench huggingface dataset:
Problem: 2050E - Three Strings
Language: python

Solution 1 (runtime: 187 ms):
for _ in range(int(input())):
    a = input()
    b = input()
    c = input()
    n, m = len(a), len(b)
    inf = float('inf')
    dp = [[inf] * (m + 1) for _ in range(n + 1)]
    dp[0][0] = 0
    for i in range(1, n + 1):
        dp[i][0] = dp[i - 1][0] + (a[i - 1] != c[i - 1])

    for j in range(1, m + 1):
        dp[0][j] = dp[0][j - 1] + (b[j - 1] != c[j - 1])

    for i in range(1, n + 1):
        for j in range(1, m + 1):
            dp[i][j] = min(dp[i - 1][j] + (a[i - 1] != c[i + j - 1]),
                           dp[i][j - 1] + (b[j - 1] != c[i + j - 1]))
    print(dp[n][m])

Solution 2 (runtime: 2390 ms):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10000)

############ ---- Input Functions ---- ############
def inp():
    return(int(input()))
def inlt():
    return(list(map(int,input().split())))
def insr():
    s = input()
    return(list(s[:len(s) - 1]))
def invr():
    return(map(int,input().split()))


def count_changes(a, b, c, i, j, k, memo_arr):
    if memo_arr[i][j] == -1:
        if k == len(c):
            memo_arr[i][j] = 0
        elif i == len(a):
            ans = 0
            for count in range(len(b[j:])):
                if b[j + count] != c[k+count]:
                    ans += 1
            memo_arr[i][j] = ans
        elif j == len(b):           
            ans = 0
            for count in range(len(a[i:])):
                if a[i + count] != c[k+count]:
                    ans += 1
            memo_arr[i][j] = ans
        else:
            case1 = count_changes(a, b, c, i+1, j, k+1, memo_arr)
            if a[i] != c[k]:
                case1 += 1
            case2 = count_changes(a, b, c, i, j+1, k+1, memo_arr)
            if b[j] != c[k]:
                case2 += 1
            memo_arr[i][j] = min(case1, case2)
    
    return memo_arr[i][j]


t = inp()
for _ in range(t):
    a = insr()
    b = insr()
    c = insr()

    i = 0
    j = 0

    k = 0
    memo_arr = [[-1 for _ in range(len(b)+1)] for _ in range(len(a) + 1)]
    print(count_changes(a, b, c, i, j, k, memo_arr))
Problem: 2050E - Three Strings
Language: python

Solution 1 (runtime: 187 ms):
for _ in range(int(input())):
    a = input()
    b = input()
    c = input()
    n, m = len(a), len(b)
    inf = float('inf')
    dp = [[inf] * (m + 1) for _ in range(n + 1)]
    dp[0][0] = 0
    for i in range(1, n + 1):
        dp[i][0] = dp[i - 1][0] + (a[i - 1] != c[i - 1])

    for j in range(1, m + 1):
        dp[0][j] = dp[0][j - 1] + (b[j - 1] != c[j - 1])

    for i in range(1, n + 1):
        for j in range(1, m + 1):
            dp[i][j] = min(dp[i - 1][j] + (a[i - 1] != c[i + j - 1]),
                           dp[i][j - 1] + (b[j - 1] != c[i + j - 1]))
    print(dp[n][m])

Solution 2 (runtime: 1374 ms):
FILE = False


class Solution:
    def __init__(self) -> None:
        pass

    def solve(self, a: str, b: str, c: str) -> int:
        dp = [[0 for _ in range(len(b) + 1)] for _ in range(len(a) + 1)]

        top_row = dp[0]
        for i_b in range(1, len(b) + 1):
            i_c = i_b
            top_row[i_b] = top_row[i_b - 1] + (1 if c[i_c - 1] != b[i_b - 1] else 0)

        for i_a in range(1, len(a) + 1):
            i_c = i_a
            dp[i_a][0] = dp[i_a - 1][0] + (1 if c[i_c - 1] != a[i_a - 1] else 0)

        for i_a in range(1, len(a) + 1):
            for i_b in range(1, len(b) + 1):
                i_c = i_a + i_b
                dp[i_a][i_b] = min(
                    dp[i_a - 1][i_b] + (1 if c[i_c - 1] != a[i_a - 1] else 0),
                    dp[i_a][i_b - 1] + (1 if c[i_c - 1] != b[i_b - 1] else 0),
                )

        return dp[-1][-1]


if __name__ == "__main__":
    if FILE:
        FILE_NAME = "CF_991_div3/inputs/e.txt"
        lines = iter(open(FILE_NAME, "r").read().splitlines())
        get_input = lambda: next(lines)
    else:
        get_input = input

    n_test_cases = int(get_input())
    for _ in range(n_test_cases):
        solution = Solution()
        a, b, c = get_input(), get_input(), get_input()
        print(solution.solve(a, b, c))
Problem: 2050E - Three Strings
Language: python

Solution 1 (runtime: 187 ms):
for _ in range(int(input())):
    a = input()
    b = input()
    c = input()
    n, m = len(a), len(b)
    inf = float('inf')
    dp = [[inf] * (m + 1) for _ in range(n + 1)]
    dp[0][0] = 0
    for i in range(1, n + 1):
        dp[i][0] = dp[i - 1][0] + (a[i - 1] != c[i - 1])

    for j in range(1, m + 1):
        dp[0][j] = dp[0][j - 1] + (b[j - 1] != c[j - 1])

    for i in range(1, n + 1):
        for j in range(1, m + 1):
            dp[i][j] = min(dp[i - 1][j] + (a[i - 1] != c[i + j - 1]),
                           dp[i][j - 1] + (b[j - 1] != c[i + j - 1]))
    print(dp[n][m])

Solution 2 (runtime: 1781 ms):
def solve():
    # Read input strings
    s1 = input()
    s2 = input()
    s3 = input()
    
    n = len(s1)
    m = len(s2)
    
    # If lengths don't match the requirements, it's impossible
    if len(s3) != n + m:
        print(-1)
        return
        
    # Create DP table
    dp = [[float('inf')] * (m + 1) for _ in range(n + 1)]
    dp[0][0] = 0  # Base case: empty strings need 0 changes
    
    # Fill dp table
    for i in range(n + 1):
        for j in range(m + 1):
            if i + j == 0:
                continue
                
            # Current position in s3 we're trying to match
            k = i + j - 1
            
            # Try taking from s1
            if i > 0:
                cost = 0 if s1[i-1] == s3[k] else 1
                dp[i][j] = min(dp[i][j], dp[i-1][j] + cost)
            
            # Try taking from s2
            if j > 0:
                cost = 0 if s2[j-1] == s3[k] else 1
                dp[i][j] = min(dp[i][j], dp[i][j-1] + cost)
    
    # Answer is in dp[n][m]
    ans = dp[n][m]
    print(ans if ans != float('inf') else -1)

if __name__ == '__main__':
    t = int(input())
    while t > 0:
        solve()
        t -= 1
Problem: 2050E - Three Strings
Language: python

Solution 1 (runtime: 187 ms):
for _ in range(int(input())):
    a = input()
    b = input()
    c = input()
    n, m = len(a), len(b)
    inf = float('inf')
    dp = [[inf] * (m + 1) for _ in range(n + 1)]
    dp[0][0] = 0
    for i in range(1, n + 1):
        dp[i][0] = dp[i - 1][0] + (a[i - 1] != c[i - 1])

    for j in range(1, m + 1):
        dp[0][j] = dp[0][j - 1] + (b[j - 1] != c[j - 1])

    for i in range(1, n + 1):
        for j in range(1, m + 1):
            dp[i][j] = min(dp[i - 1][j] + (a[i - 1] != c[i + j - 1]),
                           dp[i][j - 1] + (b[j - 1] != c[i + j - 1]))
    print(dp[n][m])

Solution 2 (runtime: 186 ms):
t=int(input())
for _ in range(t):
  a=input()
  b=input()
  c=input()
  ans=float('inf')
  dp=[[-1 for i in range(len(b)+1)] for j in range(len(a)+1)]
  dp[0][0]=0
  for i in range(1,len(b)+1):
    dp[0][i]=dp[0][i-1]+ (b[i-1]!=c[i-1])
  for i in range(1,len(a)+1):
    dp[i][0]=dp[i-1][0]+(a[i-1]!=c[i-1])
  for i in range(1,len(a)+1):
    for j in range(1,len(b)+1):
      p=dp[i-1][j]+(a[i-1]!=c[i+j-1])
      q=dp[i][j-1]+(b[j-1]!=c[i+j-1])
      dp[i][j]=min(p,q)
  print(dp[len(a)][len(b)])
Problem: 2050E - Three Strings
Language: python

Solution 1 (runtime: 187 ms):
for _ in range(int(input())):
    a = input()
    b = input()
    c = input()
    n, m = len(a), len(b)
    inf = float('inf')
    dp = [[inf] * (m + 1) for _ in range(n + 1)]
    dp[0][0] = 0
    for i in range(1, n + 1):
        dp[i][0] = dp[i - 1][0] + (a[i - 1] != c[i - 1])

    for j in range(1, m + 1):
        dp[0][j] = dp[0][j - 1] + (b[j - 1] != c[j - 1])

    for i in range(1, n + 1):
        for j in range(1, m + 1):
            dp[i][j] = min(dp[i - 1][j] + (a[i - 1] != c[i + j - 1]),
                           dp[i][j - 1] + (b[j - 1] != c[i + j - 1]))
    print(dp[n][m])

Solution 2 (runtime: 281 ms):
def min_changes(a, b, c):
    len_a = len(a)
    len_b = len(b)
    len_c = len(c)
    
    # Initialize the DP table
    dp = [[float('inf')] * (len_b + 1) for _ in range(len_a + 1)]
    dp[0][0] = 0  # Base case, no change needed for empty strings
    
    # Fill the DP table
    for i in range(len_a + 1):
        for j in range(len_b + 1):
            if i + j > len_c:
                continue
            if i < len_a and a[i] == c[i + j]:
                dp[i + 1][j] = min(dp[i + 1][j], dp[i][j])
            if j < len_b and b[j] == c[i + j]:
                dp[i][j + 1] = min(dp[i][j + 1], dp[i][j])
            if i < len_a and a[i] != c[i + j]:
                dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + 1)
            if j < len_b and b[j] != c[i + j]:
                dp[i][j + 1] = min(dp[i][j + 1], dp[i][j] + 1)
    
    return dp[len_a][len_b]

# Input
t = int(input())  # Number of test cases
for _ in range(t):
    a = input().strip()  # First string
    b = input().strip()  # Second string
    c = input().strip()  # Third string
    
    # Compute the result
    result = min_changes(a, b, c)
    
    # Output the result
    print(result)
Problem: 2050E - Three Strings
Language: python

Solution 1 (runtime: 187 ms):
for _ in range(int(input())):
    a = input()
    b = input()
    c = input()
    n, m = len(a), len(b)
    inf = float('inf')
    dp = [[inf] * (m + 1) for _ in range(n + 1)]
    dp[0][0] = 0
    for i in range(1, n + 1):
        dp[i][0] = dp[i - 1][0] + (a[i - 1] != c[i - 1])

    for j in range(1, m + 1):
        dp[0][j] = dp[0][j - 1] + (b[j - 1] != c[j - 1])

    for i in range(1, n + 1):
        for j in range(1, m + 1):
            dp[i][j] = min(dp[i - 1][j] + (a[i - 1] != c[i + j - 1]),
                           dp[i][j - 1] + (b[j - 1] != c[i + j - 1]))
    print(dp[n][m])

Solution 2 (runtime: 2389 ms):
az = int(input())
for _ in range(az):
    s1 = input()
    s2 = input()
    s3 = input()

    if len(s1) + len(s2) != len(s3):
        print(-1)
        continue

    # DP table to store minimum cost
    dp = [[float('inf')] * (len(s2) + 1) for _ in range(len(s1) + 1)]
    dp[0][0] = 0

    # Fill the DP table
    for i in range(len(s1) + 1):
        for j in range(len(s2) + 1):
            if i > 0:  # Taking a character from s1
                dp[i][j] = min(dp[i][j], dp[i - 1][j] + (s1[i - 1] != s3[i + j - 1]))
            if j > 0:  # Taking a character from s2
                dp[i][j] = min(dp[i][j], dp[i][j - 1] + (s2[j - 1] != s3[i + j - 1]))

    # Output the result for this test case
    print(dp[len(s1)][len(s2)])
Problem: 2050E - Three Strings
Language: python

Solution 1 (runtime: 187 ms):
for _ in range(int(input())):
    a = input()
    b = input()
    c = input()
    n, m = len(a), len(b)
    inf = float('inf')
    dp = [[inf] * (m + 1) for _ in range(n + 1)]
    dp[0][0] = 0
    for i in range(1, n + 1):
        dp[i][0] = dp[i - 1][0] + (a[i - 1] != c[i - 1])

    for j in range(1, m + 1):
        dp[0][j] = dp[0][j - 1] + (b[j - 1] != c[j - 1])

    for i in range(1, n + 1):
        for j in range(1, m + 1):
            dp[i][j] = min(dp[i - 1][j] + (a[i - 1] != c[i + j - 1]),
                           dp[i][j - 1] + (b[j - 1] != c[i + j - 1]))
    print(dp[n][m])

Solution 2 (runtime: 218 ms):
import os
import sys
import math
from collections import defaultdict




IS_LOCAL = False
try:
    current_directory = os.path.dirname(os.path.abspath(__file__))
    if 'cp_cursed' in current_directory:
        IS_LOCAL = True
except NameError:
    pass
if IS_LOCAL:
    sys.stdout=open('outputf.in', 'w')
    sys.stdin=open('inputf.in', 'r')

lol=lambda func=int: list(map(func, sys.stdin.readline().split()))
lol1=lambda: sys.stdin.readline().split()


T=lol()[0]
for _ in range(T):
    s1=lol1()[0]
    s2=lol1()[0]
    s3=lol1()[0]
    dp=[[i+j for j in range(len(s2)+1)] for i in range(len(s1)+1)]
    for i in range(-1, len(s1)):
        for j in range(-1, len(s2)):            
            if i!=-1:
                if s1[i]==s3[i+j+1]:
                    dp[i+1][j+1]=min(dp[i+1][j+1],dp[i][j+1])
                dp[i+1][j+1]=min(dp[i+1][j+1],dp[i][j+1]+1)
            if j!=-1:
                if s2[j]==s3[i+j+1]:
                    dp[i+1][j+1]=min(dp[i+1][j+1],dp[i+1][j])
                dp[i+1][j+1]=min(dp[i+1][j+1],dp[i+1][j]+1)
    print(dp[-1][-1])
Problem: 2050E - Three Strings
Language: python

Solution 1 (runtime: 187 ms):
for _ in range(int(input())):
    a = input()
    b = input()
    c = input()
    n, m = len(a), len(b)
    inf = float('inf')
    dp = [[inf] * (m + 1) for _ in range(n + 1)]
    dp[0][0] = 0
    for i in range(1, n + 1):
        dp[i][0] = dp[i - 1][0] + (a[i - 1] != c[i - 1])

    for j in range(1, m + 1):
        dp[0][j] = dp[0][j - 1] + (b[j - 1] != c[j - 1])

    for i in range(1, n + 1):
        for j in range(1, m + 1):
            dp[i][j] = min(dp[i - 1][j] + (a[i - 1] != c[i + j - 1]),
                           dp[i][j - 1] + (b[j - 1] != c[i + j - 1]))
    print(dp[n][m])

Solution 2 (runtime: 1467 ms):

def solve(a,b,c):
	dp = [[0] * (len(b) + 1) for _ in (range(len(a)+1))]

	d = 0
	for i in reversed(range(0,len(a))):
		if a[i] != c[i+len(b)]:
			d += 1
		dp[i][len(b)] = d
	d = 0
	for j in reversed(range(0,len(b))):
		if b[j] != c[len(a)+j]:
			d += 1
		dp[len(a)][j] = d

	for k in reversed(range(0,len(a)+len(b)-1)):
		x0 = max(0,k-(len(b)-1))
		x1 = min(k,(len(a)-1))
		for i in range(x0,x1+1):
			j = k-i
			res = None
			if i == len(a):
				res = sum(map(lambda x: x[0]!=x[1], zip(iter(b[j:]),iter(c[i+j:]))))
			elif j == len(b):
				res = sum(map(lambda x: x[0]!=x[1], zip(iter(a[i:]),iter(c[i+j:]))))
			else:
				res = min(
					dp[i+1][j] + (0 if a[i] == c[i+j] else 1),
					dp[i][j+1] + (0 if b[j] == c[i+j] else 1)
				)

			dp[i][j] = res

	return dp[0][0]

def test():
	print(solve("a","b","cb"))
	print(solve("ab","cd","acbd"))
	print(solve("ab","ba","aabb"))
	print(solve("xxx","yyy","xyxyxy"))
	print(solve("a","bcd","decf"))
	print(solve("codes","horse","codeforces"))
	print(solve("egg","annie","egaegaeg"))
	exit()

for _ in range(int(input())):
	print(solve(input(),input(),input()))
Problem: 2050E - Three Strings
Language: python

Solution 1 (runtime: 187 ms):
for _ in range(int(input())):
    a = input()
    b = input()
    c = input()
    n, m = len(a), len(b)
    inf = float('inf')
    dp = [[inf] * (m + 1) for _ in range(n + 1)]
    dp[0][0] = 0
    for i in range(1, n + 1):
        dp[i][0] = dp[i - 1][0] + (a[i - 1] != c[i - 1])

    for j in range(1, m + 1):
        dp[0][j] = dp[0][j - 1] + (b[j - 1] != c[j - 1])

    for i in range(1, n + 1):
        for j in range(1, m + 1):
            dp[i][j] = min(dp[i - 1][j] + (a[i - 1] != c[i + j - 1]),
                           dp[i][j - 1] + (b[j - 1] != c[i + j - 1]))
    print(dp[n][m])

Solution 2 (runtime: 1687 ms):
def min_changes(a, b, c):
    n, m = len(a), len(b)
    dp = [[float('inf')] * (m + 1) for _ in range(n + 1)]
    dp[0][0] = 0

    for i in range(n + 1):
        for j in range(m + 1):
            if i > 0:
                dp[i][j] = min(dp[i][j], dp[i-1][j] + (a[i-1] != c[i + j - 1]))
            if j > 0:
                dp[i][j] = min(dp[i][j], dp[i][j-1] + (b[j-1] != c[i + j - 1]))
    
    return dp[n][m]

t = int(input())
for _ in range(t):
    a = input().strip()
    b = input().strip()
    c = input().strip()
    print(min_changes(a, b, c))
Problem: 2050E - Three Strings
Language: python

Solution 1 (runtime: 187 ms):
for _ in range(int(input())):
    a = input()
    b = input()
    c = input()
    n, m = len(a), len(b)
    inf = float('inf')
    dp = [[inf] * (m + 1) for _ in range(n + 1)]
    dp[0][0] = 0
    for i in range(1, n + 1):
        dp[i][0] = dp[i - 1][0] + (a[i - 1] != c[i - 1])

    for j in range(1, m + 1):
        dp[0][j] = dp[0][j - 1] + (b[j - 1] != c[j - 1])

    for i in range(1, n + 1):
        for j in range(1, m + 1):
            dp[i][j] = min(dp[i - 1][j] + (a[i - 1] != c[i + j - 1]),
                           dp[i][j - 1] + (b[j - 1] != c[i + j - 1]))
    print(dp[n][m])

Solution 2 (runtime: 1702 ms):
def min_changes(t, cases):
    results = []
    
    for a, b, c in cases:
        len_a, len_b = len(a), len(b)
        
        # Создаем DP таблицу
        dp = [[float('inf')] * (len_b + 1) for _ in range(len_a + 1)]
        dp[0][0] = 0  # Базовый случай: ничего не нужно менять, если строка пустая
        
        for i in range(len_a + 1):
            for j in range(len_b + 1):
                if i < len_a:  # Если можно использовать следующий символ из a
                    change_cost = 0 if c[i + j] == a[i] else 1
                    dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + change_cost)
                    
                if j < len_b:  # Если можно использовать следующий символ из b
                    change_cost = 0 if c[i + j] == b[j] else 1
                    dp[i][j + 1] = min(dp[i][j + 1], dp[i][j] + change_cost)
        
        results.append(dp[len_a][len_b])
    
    return results

# Чтение входных данных
t = int(input())
cases = [tuple(input().strip() for _ in range(3)) for _ in range(t)]

# Получение результатов
results = min_changes(t, cases)

# Печать результатов
for result in results:
    print(result)
Problem: 2050E - Three Strings
Language: python

Solution 1 (runtime: 187 ms):
for _ in range(int(input())):
    a = input()
    b = input()
    c = input()
    n, m = len(a), len(b)
    inf = float('inf')
    dp = [[inf] * (m + 1) for _ in range(n + 1)]
    dp[0][0] = 0
    for i in range(1, n + 1):
        dp[i][0] = dp[i - 1][0] + (a[i - 1] != c[i - 1])

    for j in range(1, m + 1):
        dp[0][j] = dp[0][j - 1] + (b[j - 1] != c[j - 1])

    for i in range(1, n + 1):
        for j in range(1, m + 1):
            dp[i][j] = min(dp[i - 1][j] + (a[i - 1] != c[i + j - 1]),
                           dp[i][j - 1] + (b[j - 1] != c[i + j - 1]))
    print(dp[n][m])

Solution 2 (runtime: 2171 ms):
def min_changes_to_interleave(test_cases):
    results = []
    
    for a, b, c in test_cases:
        n, m = len(a), len(b)
        dp = [[float('inf')] * (m + 1) for _ in range(n + 1)]
        
        dp[0][0] = 0
        
        for i in range(n + 1):
            for j in range(m + 1):
                if i + j > len(c):
                    continue
                
                if i > 0:
                    dp[i][j] = min(dp[i][j], dp[i - 1][j] + (c[i + j - 1] != a[i - 1]))
                
                if j > 0:
                    dp[i][j] = min(dp[i][j], dp[i][j - 1] + (c[i + j - 1] != b[j - 1]))
        
        results.append(dp[n][m])
    
    return results

# Example usage
t = int(input())
test_cases = [tuple(input().strip() for _ in range(3)) for _ in range(t)]
results = min_changes_to_interleave(test_cases)

for result in results:
    print(result)
Problem: 2050E - Three Strings
Language: python

Solution 1 (runtime: 187 ms):
for _ in range(int(input())):
    a = input()
    b = input()
    c = input()
    n, m = len(a), len(b)
    inf = float('inf')
    dp = [[inf] * (m + 1) for _ in range(n + 1)]
    dp[0][0] = 0
    for i in range(1, n + 1):
        dp[i][0] = dp[i - 1][0] + (a[i - 1] != c[i - 1])

    for j in range(1, m + 1):
        dp[0][j] = dp[0][j - 1] + (b[j - 1] != c[j - 1])

    for i in range(1, n + 1):
        for j in range(1, m + 1):
            dp[i][j] = min(dp[i - 1][j] + (a[i - 1] != c[i + j - 1]),
                           dp[i][j - 1] + (b[j - 1] != c[i + j - 1]))
    print(dp[n][m])

Solution 2 (runtime: 374 ms):
for _ in range(int(input())):
    a = input()
    b = input()
    c = input()

    m = len(a)
    n = len(b)
    
    a = '$' + a
    b = '$' + b
    c = '$' + c

    dp = [[None for j in range(n + 1)] for i in range(m + 1)]
    dp[0][0] = 0

    for i in range(1, m + 1):
        if a[i] == c[i]:
            dp[i][0] = dp[i - 1][0]
        else:
            dp[i][0] = 1 + dp[i - 1][0]

    for j in range(1, n + 1):
        if b[j] == c[j]:
            dp[0][j] = dp[0][j - 1]
        else:
            dp[0][j] = 1 + dp[0][j - 1]

    for i in range(1, m + 1):
        for j in range(1, n + 1):
            if a[i] == c[i + j]:
                if b[j] == c[i + j]:
                    dp[i][j] = min(dp[i - 1][j], dp[i][j - 1])
                else:
                    dp[i][j] = min(dp[i - 1][j], 1 + dp[i][j - 1])
            else:
                if b[j] == c[i + j]:
                    dp[i][j] = min(1 + dp[i - 1][j], dp[i][j - 1])
                else:
                    dp[i][j] = min(1 + dp[i - 1][j], 1 + dp[i][j - 1])

    print(dp[m][n])
Problem: 2050E - Three Strings
Language: python

Solution 1 (runtime: 187 ms):
for _ in range(int(input())):
    a = input()
    b = input()
    c = input()
    n, m = len(a), len(b)
    inf = float('inf')
    dp = [[inf] * (m + 1) for _ in range(n + 1)]
    dp[0][0] = 0
    for i in range(1, n + 1):
        dp[i][0] = dp[i - 1][0] + (a[i - 1] != c[i - 1])

    for j in range(1, m + 1):
        dp[0][j] = dp[0][j - 1] + (b[j - 1] != c[j - 1])

    for i in range(1, n + 1):
        for j in range(1, m + 1):
            dp[i][j] = min(dp[i - 1][j] + (a[i - 1] != c[i + j - 1]),
                           dp[i][j - 1] + (b[j - 1] != c[i + j - 1]))
    print(dp[n][m])

Solution 2 (runtime: 2046 ms):
def f(a,b,c,p,q,dp):
    dp[0][0] = 0
    for i in range(p+1):
        for j in range(q+1):
            if i > 0 :
                val = int(a[i-1] != c[i+j-1])
                dp[i][j] = min(dp[i][j],dp[i-1][j]+val)
            if j > 0 :
                val = int(b[j-1] != c[i+j-1])
                dp[i][j] = min(dp[i][j] , dp[i][j-1]+val)
    return dp[p][q]
                

n = int(input())
for _ in range(n):
    a = input()
    b = input()
    c = input()
    i = len(a)
    j = len(b)
    dp = [[float('inf')] * (j+1) for _ in range(i+1)]
    res = f(a, b, c, i, j, dp)
    print(res)
Problem: 2050E - Three Strings
Language: python

Solution 1 (runtime: 187 ms):
for _ in range(int(input())):
    a = input()
    b = input()
    c = input()
    n, m = len(a), len(b)
    inf = float('inf')
    dp = [[inf] * (m + 1) for _ in range(n + 1)]
    dp[0][0] = 0
    for i in range(1, n + 1):
        dp[i][0] = dp[i - 1][0] + (a[i - 1] != c[i - 1])

    for j in range(1, m + 1):
        dp[0][j] = dp[0][j - 1] + (b[j - 1] != c[j - 1])

    for i in range(1, n + 1):
        for j in range(1, m + 1):
            dp[i][j] = min(dp[i - 1][j] + (a[i - 1] != c[i + j - 1]),
                           dp[i][j - 1] + (b[j - 1] != c[i + j - 1]))
    print(dp[n][m])

Solution 2 (runtime: 249 ms):
def main():
    import sys
    input = sys.stdin.read
    data = input().splitlines()
    t = int(data[0])
    idx = 1
    for _ in range(t):
        a = data[idx]
        b = data[idx + 1]
        c = data[idx + 2]
        idx += 3
        len_a = len(a)
        len_b = len(b)
        len_c = len(c)
        INF = float('inf')
        dp = [[INF] * (len_b + 1) for _ in range(len_a + 1)]
        dp[0][0] = 0
        for sum_ in range(len_a + len_b):
            for j in range(max(0, sum_ - len_a), min(sum_, len_b) + 1):
                i = sum_ - j
                if i > len_a or j > len_b:
                    continue
                if i < len_a and j < len_b:
                    cost_a = 0 if a[i] == c[i + j] else 1
                    dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + cost_a)
                    cost_b = 0 if b[j] == c[i + j] else 1
                    dp[i][j + 1] = min(dp[i][j + 1], dp[i][j] + cost_b)
                if i == len_a and j < len_b:
                    cost_b = 0 if b[j] == c[i + j] else 1
                    dp[i][j + 1] = min(dp[i][j + 1], dp[i][j] + cost_b)
                if j == len_b and i < len_a:
                    cost_a = 0 if a[i] == c[i + j] else 1
                    dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + cost_a)
        print(dp[len_a][len_b])

if __name__ == "__main__":
    main()