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

Solution 1 (runtime: 218 ms):
for _ in[1]*int(input()):
 s,t,u=input(),input(),input();n,m=len(s),len(t)
 inf=10**9
 d=[[inf]*(m+1)for _ in range(n+1)]
 d[0][0]=0
 for i in range(n+1):
  for j in range(m+1):
   if i==j==0:continue
   k=i+j-1
   if k>=len(u):continue
   if i>0:
    c=d[i-1][j]+(s[i-1]!=u[k])
    if c<d[i][j]:d[i][j]=c
   if j>0:
    c=d[i][j-1]+(t[j-1]!=u[k])
    if c<d[i][j]:d[i][j]=c
 print(d[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: 218 ms):
for _ in[1]*int(input()):
 s,t,u=input(),input(),input();n,m=len(s),len(t)
 inf=10**9
 d=[[inf]*(m+1)for _ in range(n+1)]
 d[0][0]=0
 for i in range(n+1):
  for j in range(m+1):
   if i==j==0:continue
   k=i+j-1
   if k>=len(u):continue
   if i>0:
    c=d[i-1][j]+(s[i-1]!=u[k])
    if c<d[i][j]:d[i][j]=c
   if j>0:
    c=d[i][j-1]+(t[j-1]!=u[k])
    if c<d[i][j]:d[i][j]=c
 print(d[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: 218 ms):
for _ in[1]*int(input()):
 s,t,u=input(),input(),input();n,m=len(s),len(t)
 inf=10**9
 d=[[inf]*(m+1)for _ in range(n+1)]
 d[0][0]=0
 for i in range(n+1):
  for j in range(m+1):
   if i==j==0:continue
   k=i+j-1
   if k>=len(u):continue
   if i>0:
    c=d[i-1][j]+(s[i-1]!=u[k])
    if c<d[i][j]:d[i][j]=c
   if j>0:
    c=d[i][j-1]+(t[j-1]!=u[k])
    if c<d[i][j]:d[i][j]=c
 print(d[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: 218 ms):
for _ in[1]*int(input()):
 s,t,u=input(),input(),input();n,m=len(s),len(t)
 inf=10**9
 d=[[inf]*(m+1)for _ in range(n+1)]
 d[0][0]=0
 for i in range(n+1):
  for j in range(m+1):
   if i==j==0:continue
   k=i+j-1
   if k>=len(u):continue
   if i>0:
    c=d[i-1][j]+(s[i-1]!=u[k])
    if c<d[i][j]:d[i][j]=c
   if j>0:
    c=d[i][j-1]+(t[j-1]!=u[k])
    if c<d[i][j]:d[i][j]=c
 print(d[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: 218 ms):
for _ in[1]*int(input()):
 s,t,u=input(),input(),input();n,m=len(s),len(t)
 inf=10**9
 d=[[inf]*(m+1)for _ in range(n+1)]
 d[0][0]=0
 for i in range(n+1):
  for j in range(m+1):
   if i==j==0:continue
   k=i+j-1
   if k>=len(u):continue
   if i>0:
    c=d[i-1][j]+(s[i-1]!=u[k])
    if c<d[i][j]:d[i][j]=c
   if j>0:
    c=d[i][j-1]+(t[j-1]!=u[k])
    if c<d[i][j]:d[i][j]=c
 print(d[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: 218 ms):
for _ in[1]*int(input()):
 s,t,u=input(),input(),input();n,m=len(s),len(t)
 inf=10**9
 d=[[inf]*(m+1)for _ in range(n+1)]
 d[0][0]=0
 for i in range(n+1):
  for j in range(m+1):
   if i==j==0:continue
   k=i+j-1
   if k>=len(u):continue
   if i>0:
    c=d[i-1][j]+(s[i-1]!=u[k])
    if c<d[i][j]:d[i][j]=c
   if j>0:
    c=d[i][j-1]+(t[j-1]!=u[k])
    if c<d[i][j]:d[i][j]=c
 print(d[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: 218 ms):
for _ in[1]*int(input()):
 s,t,u=input(),input(),input();n,m=len(s),len(t)
 inf=10**9
 d=[[inf]*(m+1)for _ in range(n+1)]
 d[0][0]=0
 for i in range(n+1):
  for j in range(m+1):
   if i==j==0:continue
   k=i+j-1
   if k>=len(u):continue
   if i>0:
    c=d[i-1][j]+(s[i-1]!=u[k])
    if c<d[i][j]:d[i][j]=c
   if j>0:
    c=d[i][j-1]+(t[j-1]!=u[k])
    if c<d[i][j]:d[i][j]=c
 print(d[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()
Problem: 2050E - Three Strings
Language: python

Solution 1 (runtime: 218 ms):
for _ in[1]*int(input()):
 s,t,u=input(),input(),input();n,m=len(s),len(t)
 inf=10**9
 d=[[inf]*(m+1)for _ in range(n+1)]
 d[0][0]=0
 for i in range(n+1):
  for j in range(m+1):
   if i==j==0:continue
   k=i+j-1
   if k>=len(u):continue
   if i>0:
    c=d[i-1][j]+(s[i-1]!=u[k])
    if c<d[i][j]:d[i][j]=c
   if j>0:
    c=d[i][j-1]+(t[j-1]!=u[k])
    if c<d[i][j]:d[i][j]=c
 print(d[n][m])

Solution 2 (runtime: 202 ms):
tc = int(input())
for i in range(tc):
    a = input()
    b = input()
    c = input()
    m = len(a); n = len(b)
    dp = []
    for i in range(m+1):
        dp.append([0] * (n+1))
    for i in range(1, m+1):
        dp[i][0] = dp[i-1][0] + (a[i-1] != c[i-1])
    for j in range(1, n+1):
        dp[0][j] = dp[0][j-1] + (b[j-1] != c[j-1])
    for i in range(1, m+1):
        for j in range(1, n+1):
            length = i+j
            latest = c[length-1]
            dp[i][j] = min(dp[i-1][j] + (a[i-1] != latest), dp[i][j-1] + (b[j-1] != latest))
    print(int(dp[m][n]))
Problem: 2050E - Three Strings
Language: python

Solution 1 (runtime: 218 ms):
for _ in[1]*int(input()):
 s,t,u=input(),input(),input();n,m=len(s),len(t)
 inf=10**9
 d=[[inf]*(m+1)for _ in range(n+1)]
 d[0][0]=0
 for i in range(n+1):
  for j in range(m+1):
   if i==j==0:continue
   k=i+j-1
   if k>=len(u):continue
   if i>0:
    c=d[i-1][j]+(s[i-1]!=u[k])
    if c<d[i][j]:d[i][j]=c
   if j>0:
    c=d[i][j-1]+(t[j-1]!=u[k])
    if c<d[i][j]:d[i][j]=c
 print(d[n][m])

Solution 2 (runtime: 2484 ms):
import sys
input = lambda: sys.stdin.buffer.readline().decode().strip()
sys.setrecursionlimit(100000)


for _ in range(int(input())):
    a = input(); n = len(a)
    b = input(); m = len(b)
    c = input()
    dp = [[float('inf')]*(m + 1) for _ in range(n + 1)]

    def solve(i, j):
        if i + j == n + m: return 0
        if dp[i][j] == float('inf'):
            ans1 = (solve(i + 1, j) + (a[i] != c[i + j])) if i < n else float('inf')
            ans2 = (solve(i, j + 1) + (b[j] != c[i + j])) if j < m else float('inf')
            dp[i][j] = min(ans1, ans2)
        return dp[i][j]

    print(solve(0, 0))
Problem: 2050E - Three Strings
Language: python

Solution 1 (runtime: 218 ms):
for _ in[1]*int(input()):
 s,t,u=input(),input(),input();n,m=len(s),len(t)
 inf=10**9
 d=[[inf]*(m+1)for _ in range(n+1)]
 d[0][0]=0
 for i in range(n+1):
  for j in range(m+1):
   if i==j==0:continue
   k=i+j-1
   if k>=len(u):continue
   if i>0:
    c=d[i-1][j]+(s[i-1]!=u[k])
    if c<d[i][j]:d[i][j]=c
   if j>0:
    c=d[i][j-1]+(t[j-1]!=u[k])
    if c<d[i][j]:d[i][j]=c
 print(d[n][m])

Solution 2 (runtime: 2264 ms):
import sys
sys.setrecursionlimit(10**6)
testcases = int(input())
for testcase in range(testcases):
    a = str(input())
    b = str(input())
    c = str(input())
    dp = [[-1 for j in range(len(b) + 1)] for i in range(len(a) + 1)]
    def solve(ptr1, ptr2, a, b, c, dp)-> int:
        if ptr1 == len(a) and ptr2 == len(b):
            return 0
        if dp[ptr1][ptr2] != -1:
            return dp[ptr1][ptr2]
        minChange = 2001
        usingA, usingB = 2001, 2001
        if ptr1 < len(a):
            usingA = solve(ptr1 + 1, ptr2, a, b, c, dp)
            if a[ptr1] != c[ptr1 + ptr2]:
                usingA += 1
        if ptr2 < len(b):
            usingB = solve(ptr1, ptr2 + 1, a, b, c, dp)
            if b[ptr2] != c[ptr1 + ptr2]:
                usingB += 1 
        dp[ptr1][ptr2] = min(usingA, usingB)
        return dp[ptr1][ptr2]
    minChange = solve(0, 0, a, b, c, dp)
    print(minChange)
Problem: 2050E - Three Strings
Language: python

Solution 1 (runtime: 218 ms):
for _ in[1]*int(input()):
 s,t,u=input(),input(),input();n,m=len(s),len(t)
 inf=10**9
 d=[[inf]*(m+1)for _ in range(n+1)]
 d[0][0]=0
 for i in range(n+1):
  for j in range(m+1):
   if i==j==0:continue
   k=i+j-1
   if k>=len(u):continue
   if i>0:
    c=d[i-1][j]+(s[i-1]!=u[k])
    if c<d[i][j]:d[i][j]=c
   if j>0:
    c=d[i][j-1]+(t[j-1]!=u[k])
    if c<d[i][j]:d[i][j]=c
 print(d[n][m])

Solution 2 (runtime: 187 ms):
t=int(input())
for _ in range(t):
    a=input()
    b=input()
    c=input()
    dp=[[10**9 for j in range(len(b)+1)]for i in range(len(a)+1)]
    dp[0][0]=0
    i=0
    j=0
    for i in range(len(a)+1):
        for j in range(len(b)+1):
            
            if i==0 and j==0 :
                continue
            cost1=0
            cost2=0
            if i != 0 and a[i-1]!=c[i+j-1]:
                cost1=1
            if j!=0 and b[j-1]!=c[i+j-1]:
                cost2=1
            if i==0:
                dp[i][j]=dp[i][j-1]+cost2
            if j==0:
                dp[i][j]=dp[i-1][j]+cost1
            else:
                dp[i][j]=min(dp[i-1][j]+cost1,dp[i][j-1]+cost2)

    print(dp[len(a)][len(b)])
Problem: 2050E - Three Strings
Language: python

Solution 1 (runtime: 218 ms):
for _ in[1]*int(input()):
 s,t,u=input(),input(),input();n,m=len(s),len(t)
 inf=10**9
 d=[[inf]*(m+1)for _ in range(n+1)]
 d[0][0]=0
 for i in range(n+1):
  for j in range(m+1):
   if i==j==0:continue
   k=i+j-1
   if k>=len(u):continue
   if i>0:
    c=d[i-1][j]+(s[i-1]!=u[k])
    if c<d[i][j]:d[i][j]=c
   if j>0:
    c=d[i][j-1]+(t[j-1]!=u[k])
    if c<d[i][j]:d[i][j]=c
 print(d[n][m])

Solution 2 (runtime: 671 ms):
for _ in range(int(input())):
    a=input()
    b=input()
    c=input()
    A=len(a)
    B=len(b)
    C=len(c)
    dp=[[10**10 for _ in range(A+1)]for _ in range(C+1)]
    dp[0][0]=0
    for i in range(C):
        for j in range(A+1):
            if j<A:
                if c[i]==a[j]:
                    dp[i+1][j+1]=min(dp[i][j],dp[i+1][j+1])
                else:
                    dp[i+1][j+1]=min(dp[i][j]+1,dp[i+1][j+1])
            if 0<=i-j<B:
                if c[i]==b[i-j]:
                    dp[i+1][j]=min(dp[i][j],dp[i+1][j])
                else:
                    dp[i+1][j]=min(dp[i][j]+1,dp[i+1][j])
    print(min(dp[-1]))
Problem: 2050E - Three Strings
Language: python

Solution 1 (runtime: 218 ms):
for _ in[1]*int(input()):
 s,t,u=input(),input(),input();n,m=len(s),len(t)
 inf=10**9
 d=[[inf]*(m+1)for _ in range(n+1)]
 d[0][0]=0
 for i in range(n+1):
  for j in range(m+1):
   if i==j==0:continue
   k=i+j-1
   if k>=len(u):continue
   if i>0:
    c=d[i-1][j]+(s[i-1]!=u[k])
    if c<d[i][j]:d[i][j]=c
   if j>0:
    c=d[i][j-1]+(t[j-1]!=u[k])
    if c<d[i][j]:d[i][j]=c
 print(d[n][m])

Solution 2 (runtime: 280 ms):
# import sys
# sys.setrecursionlimit(2*(10**3))
# for _ in range(int(input())):
#     a = input()
#     b = input()
#     c = input()
#     dic = {}

#     def calculate(i,j,k,dic):
#         if (i,j,k) in dic:
#             return dic[(i,j,k)]
#         if i == len(a):
#             count = 0
#             while k <len(c):
#                 if c[k] != b[j]:
#                     count+=1
#                 k+=1
#                 j+=1
#             dic[(i,j,k)] = count
#             return count
#         if j == len(b):
#             count = 0
#             while k < len(c):
#                 if c[k] != a[i]:
#                     count+=1
#                 i+=1
#                 k+=1
#             dic[(i,j,k)] = count
#             return count 

#         if c[k]!=a[i] and c[k]!=b[j]:
#             count = 1 + min(calculate(i+1,j,k+1,dic),calculate(i,j+1,k+1,dic))
#         elif c[k]!=a[i] and c[k] == b[j]:
#             count = min(calculate(i,j+1,k+1,dic),calculate(i+1,j,k+1,dic)+1)
#         elif c[k]==a[i] and c[k] != b[j]:
#             count = min(calculate(i,j+1,k+1,dic)+1,calculate(i+1,j,k+1,dic))
#         else:
#             count = min(calculate(i+1,j,k+1,dic),calculate(i,j+1,k+1,dic))

#         dic[(i,j,k)] = count
#         return count    
            
#     print(calculate(0,0,0,dic))

for _ in range(int(input())):
    a = input()
    b = input()
    c = input()

    dp = [[float('inf')] * (len(b) + 1) for _ in range(len(a) + 1)]

    for i in range(len(a) + 1):
        for j in range(len(b) + 1):
            k = i + j
            if k > len(c):
                continue

            if i == 0 and j == 0:
                dp[i][j] = 0
            else:
                if i > 0 and c[k - 1] == a[i - 1]:
                    dp[i][j] = min(dp[i][j], dp[i - 1][j])
                if j > 0 and c[k - 1] == b[j - 1]:
                    dp[i][j] = min(dp[i][j], dp[i][j - 1])
                if i > 0 and c[k - 1] != a[i - 1]:
                    dp[i][j] = min(dp[i][j], dp[i - 1][j] + 1)
                if j > 0 and c[k - 1] != b[j - 1]:
                    dp[i][j] = min(dp[i][j], dp[i][j - 1] + 1)

    print(dp[len(a)][len(b)])
Problem: 2050E - Three Strings
Language: python

Solution 1 (runtime: 218 ms):
for _ in[1]*int(input()):
 s,t,u=input(),input(),input();n,m=len(s),len(t)
 inf=10**9
 d=[[inf]*(m+1)for _ in range(n+1)]
 d[0][0]=0
 for i in range(n+1):
  for j in range(m+1):
   if i==j==0:continue
   k=i+j-1
   if k>=len(u):continue
   if i>0:
    c=d[i-1][j]+(s[i-1]!=u[k])
    if c<d[i][j]:d[i][j]=c
   if j>0:
    c=d[i][j-1]+(t[j-1]!=u[k])
    if c<d[i][j]:d[i][j]=c
 print(d[n][m])

Solution 2 (runtime: 390 ms):
def min_changes(a, b, c):

    n, m = len(a), len(b)

    

    # DP array

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

            # Skip invalid states

            if dp[i][j] == float('inf'):

                continue

            

            k = i + j  # Position in c

            

            # We can take the next character from a

            if i < n:

                cost = 0 if a[i] == c[k] else 1

                dp[i+1][j] = min(dp[i+1][j], dp[i][j] + cost)

            

            # We can take the next character from b

            if j < m:

                cost = 0 if b[j] == c[k] else 1

                dp[i][j+1] = min(dp[i][j+1], dp[i][j] + cost)

    

    return dp[n][m]



def main():

    t = int(input())

    

    for _ in range(t):

        a = input().strip()

        b = input().strip()

        c = input().strip()

        

        print(min_changes(a, b, c))



if __name__ == "__main__":

    main()
Problem: 2050E - Three Strings
Language: python

Solution 1 (runtime: 218 ms):
for _ in[1]*int(input()):
 s,t,u=input(),input(),input();n,m=len(s),len(t)
 inf=10**9
 d=[[inf]*(m+1)for _ in range(n+1)]
 d[0][0]=0
 for i in range(n+1):
  for j in range(m+1):
   if i==j==0:continue
   k=i+j-1
   if k>=len(u):continue
   if i>0:
    c=d[i-1][j]+(s[i-1]!=u[k])
    if c<d[i][j]:d[i][j]=c
   if j>0:
    c=d[i][j-1]+(t[j-1]!=u[k])
    if c<d[i][j]:d[i][j]=c
 print(d[n][m])

Solution 2 (runtime: 186 ms):
t = int(input())

for _ in range(t):

    a = input()

    b = input()

    c = input()

    n = len(a)

    m = len(b)



    ## 背包？？ 区间？？X

    ## 搜索 2**n X

    ##状态转移 dp？？题解 QWQ

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

    for i in range(n):

        dp[i+1][0] = dp[i][0]+(c[i]!=a[i])

    for j in range(m):

        dp[0][j+1] = dp[0][j]+(c[j]!=b[j])

    for i in range(n):

        for j in range(m):

            dp[i+1][j+1] = min(dp[i][j+1]+(a[i]!=c[i+j+1]),dp[i+1][j]+(b[j]!=c[i+j+1]))

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

Solution 1 (runtime: 296 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 < n:
                dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + (a[i] != c[i + j]))
            if j < m:
                dp[i][j + 1] = min(dp[i][j + 1], dp[i][j] + (b[j] != c[i + j]))

    return dp[n][m]


for _ in range(int(input())):
    a = input()
    b = input()
    c = input()
    print(min_changes(a, b, c))



Solution 2 (runtime: 2421 ms):
for k in range(int(input())):
    a = input().strip()
    b = input().strip()
    c = input().strip()
    n = len(a)
    m = len(b)
    dp = [[float('inf')] * (m + 1) for i 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] + (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]))
    print(dp[n][m])
Problem: 2050E - Three Strings
Language: python

Solution 1 (runtime: 296 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 < n:
                dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + (a[i] != c[i + j]))
            if j < m:
                dp[i][j + 1] = min(dp[i][j + 1], dp[i][j] + (b[j] != c[i + j]))

    return dp[n][m]


for _ in range(int(input())):
    a = input()
    b = input()
    c = input()
    print(min_changes(a, b, c))



Solution 2 (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])