Problem: 2050E - Three Strings
Language: python

Solution 1 (runtime: 1202 ms):
# author 
# Kidus Guade

import sys
import random
from types import GeneratorType
from sys import stdin,stdout
from math import gcd,floor,sqrt,log, ceil
from collections import defaultdict, Counter, deque
from bisect import bisect_left,bisect_right, insort_left, insort_right
def ints(): return map(int, sys.stdin.readline().strip().split())
def li(): return list(map(int, sys.stdin.readline().strip().split()))
def string(): return sys.stdin.readline().strip()
def get_int(): return int(sys.stdin.readline().strip())
def li_strings(): return list(map(str, sys.stdin.readline().strip().split()))


def bootstrap(f, stack=[]):
    def wrappedfunc(*args, **kwargs):
        if stack:
            return f(*args, **kwargs)
        else:
            to = f(*args, **kwargs)
            while True:
                if type(to) is GeneratorType:
                    stack.append(to)
                    to = next(to)
                else:
                    stack.pop()
                    if not stack:
                        break
                    to = stack[-1].send(to)
            return to
    return wrappedfunc
        

def primeFactors(n):
    
    i = 2
    primfac = []
    while i * i <= n:
        while n % i == 0:
            primfac.append(i)
            n = n / i
        i = i + 1
    if n > 1:
        primfac.append(int(n))
    return primfac
def prime_sieve(n):
    is_prime = [True for _ in range(n + 1)]
    is_prime[0] = is_prime[1] = False
    i = 2
    while i * i <= n:
        if is_prime[i]:
            j = i * i
            while j <= n:
                is_prime[j] = False
                j += i
        i += 1
    return is_prime
def find_divisors(number):
    divisors = []
    for i in range(1, int(number**0.5) + 1):
        if number % i == 0:
            divisors.append(i)
            if i != number // i:
                divisors.append(number // i)
    return divisors    
def solve():
    a = string()
    b = string()
    c = string()
    dp = [[float("inf") for _ in range(len(a) + 1)] for _ in range(len(c) + 1)]
    for i in range(len(a) + 1):
        dp[len(c)][i] = 0
  
    for i in range(len(c) -1, -1, -1):
        for j in range(len(a) + 1):
            if j > i:
                continue
            k = i - j
           
            vv1, vv2= float("inf"), float("inf")
            if j  < len(a):
                if a[j] == c[i]:
                    vv1 = dp[i + 1][j +1]
                else:
                    vv1 = 1 + dp[i + 1][j + 1]
            if k < len(b):
                if b[k] == c[i]:
                    vv2 = dp[i + 1][j]
                else:
                    vv2 = 1 + dp[i + 1][j]
            
            dp[i][j] = min(min(vv1, vv2), dp[i][j])
    return dp[0][0]
    a = string()
    b = string()
    c = string()

    

   
    memo = [[float('inf') for _ in range(len(c) + 1)] for _ in range(len(a) + 1)]
    
 
    for i in range(len(a) + 1):
        memo[i][len(c)] = 0

  
    for k in range(len(c) - 1, -1, -1):
        for i in range(len(a) + 1):
            if i < len(a):
                if a[i] == c[k]:
                    vv = memo[i + 1][k + 1]
                else:
                    vv = 1 + memo[i + 1][k + 1]
                memo[i][k] = min(memo[i][k], vv)

            if k - i < len(b):
                if b[k - i] == c[k]:
                    vv = memo[i][k + 1]
                else:
                    vv = 1 + memo[i][k + 1]
                memo[i][k] = min(memo[i][k], vv)

    # The result is stored in memo[0][0]
    return memo[0][0]
    
    

# Call the solve function
# result = solve()
# print(result)
    
        
                



    
    # return dp[len(a)][len(c)]

   

    memo = [[0 for _ in range(len(c) + 1)] for _ in range(len(a) + 1)]
    def dp(i,k):
        if k == len(c):
            yield 0
        if memo[i][k]:
            yield memo[i][k]
        val = float('inf')
        if i < len(a):
            if a[i] == c[k]:
                vv = dp(i + 1,k + 1)
                
            else:
                vv = dp(i + 1,k + 1)
                vv += 1
            val = min(val, vv)
               
        if k - i < len(b):
            if b[k - i ] == c[k]:
                vv = dp(i,  k + 1)
              
            else:
                vv = dp(i, k + 1)
                vv += 1

            val = min(val, vv)
        memo[i][k] = val
        yield memo[i][k]
    return dp(0,0)

    
  


    
    
    
if __name__ == "__main__":
    for _ in range(get_int()):
        print(solve())

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))