ShaysXIA/CodeInsightBench huggingface dataset:
Problem: 2050D - Digital string maximization
Language: python

Solution 1 (runtime: 624 ms):
def solve():

    s = list(input().strip())  # Convert string to list for mutability

    n = len(s)



    for i in range(1, n):

        j, curr = i - 1, i

        while j >= 0 and s[curr] > '0' and s[curr] > chr(ord(s[j]) + 1):

            s[curr] = chr(ord(s[curr]) - 1)  # Decrease digit

            s[curr], s[j] = s[j], s[curr]  # Swap

            curr -= 1

            j = curr - 1



    print("".join(s))



# Handling multiple test cases

t = int(input())

for _ in range(t):

    solve()



Solution 2 (runtime: 156 ms):
for _ in range(int(input())):

a = input()

s = [int(digit) for digit in a]

n = len(s)

for i in range(n):

best_value = s[i]

best_idx = -1

for j in range(i + 1, min(i + 9, n)):

score = s[j]-(j - i)

if score>best_value:

best_value=score

best_idx=j



if best_idx != -1:

# print(best_value,s[best_idx],i,best_idx)

s[i], s[best_idx] = best_value, s[i]

for j in range(best_idx, i+1, -1):

s[j], s[j - 1] = s[j - 1], s[j]

# print(s)



print("".join(map(str, s)))
Problem: 2050D - Digital string maximization
Language: python

Solution 1 (runtime: 624 ms):
def solve():

    s = list(input().strip())  # Convert string to list for mutability

    n = len(s)



    for i in range(1, n):

        j, curr = i - 1, i

        while j >= 0 and s[curr] > '0' and s[curr] > chr(ord(s[j]) + 1):

            s[curr] = chr(ord(s[curr]) - 1)  # Decrease digit

            s[curr], s[j] = s[j], s[curr]  # Swap

            curr -= 1

            j = curr - 1



    print("".join(s))



# Handling multiple test cases

t = int(input())

for _ in range(t):

    solve()



Solution 2 (runtime: 171 ms):
mod = 1e9 + 7

for _ in range(int(input())):

s = list(map(int,list(input())))

for i in range(len(s)-1):

mxi = -1

mx = 0

for j in range(1,10):

if i + j >= len(s):

break

if int(s[i+j]) - j - int(s[i]) > mx:

mx = int(s[i+j]) - j - int(s[i])

mxi = j

if mxi == -1:

continue



for jj in range(i+mxi,i,-1):

s[jj],s[jj-1] = s[jj-1],s[jj] - 1



print(''.join(list(map(str,s))))
Problem: 2050D - Digital string maximization
Language: python

Solution 1 (runtime: 624 ms):
def solve():

    s = list(input().strip())  # Convert string to list for mutability

    n = len(s)



    for i in range(1, n):

        j, curr = i - 1, i

        while j >= 0 and s[curr] > '0' and s[curr] > chr(ord(s[j]) + 1):

            s[curr] = chr(ord(s[curr]) - 1)  # Decrease digit

            s[curr], s[j] = s[j], s[curr]  # Swap

            curr -= 1

            j = curr - 1



    print("".join(s))



# Handling multiple test cases

t = int(input())

for _ in range(t):

    solve()



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

def solve():

s = [int(i) for i in list(input())]

n = len(s)

for i in range(1,n,1):

for j in range(max(i-10, 0), i):

if s[i] - (i-j) > s[j]:

tmp = s[j:i]

s[j] = s[i] - (i-j)

s[j+1:i+1] = tmp

break

print("".join([str(i) for i in s]))

return

for i in range(t):

solve()
Problem: 2050D - Digital string maximization
Language: python

Solution 1 (runtime: 624 ms):
def solve():

    s = list(input().strip())  # Convert string to list for mutability

    n = len(s)



    for i in range(1, n):

        j, curr = i - 1, i

        while j >= 0 and s[curr] > '0' and s[curr] > chr(ord(s[j]) + 1):

            s[curr] = chr(ord(s[curr]) - 1)  # Decrease digit

            s[curr], s[j] = s[j], s[curr]  # Swap

            curr -= 1

            j = curr - 1



    print("".join(s))



# Handling multiple test cases

t = int(input())

for _ in range(t):

    solve()



Solution 2 (runtime: 906 ms):
def solve():

s = list(input())

n = len(s)

while True:

changed = False

for i in range(1, n):

if s[i] != '0' and int(s[i]) - 1 > int(s[i-1]):

num = int(s[i])

s[i] = str(int(s[i-1]))

s[i-1] = str(num - 1)

changed = True

if not changed:

break

print("".join(s))



t = int(input())

for _ in range(t):

solve()
Problem: 2050D - Digital string maximization
Language: python

Solution 1 (runtime: 624 ms):
def solve():

    s = list(input().strip())  # Convert string to list for mutability

    n = len(s)



    for i in range(1, n):

        j, curr = i - 1, i

        while j >= 0 and s[curr] > '0' and s[curr] > chr(ord(s[j]) + 1):

            s[curr] = chr(ord(s[curr]) - 1)  # Decrease digit

            s[curr], s[j] = s[j], s[curr]  # Swap

            curr -= 1

            j = curr - 1



    print("".join(s))



# Handling multiple test cases

t = int(input())

for _ in range(t):

    solve()



Solution 2 (runtime: 265 ms):
for _ in range(int(input())):

s = list(map(int,input()))

for i in range(9):

for j in range(len(s)-1):

if s[j] < s[j+1] - 1:

s[j],s[j+1] = s[j+1]-1,s[j]

print(*s,sep= '')
Problem: 2050D - Digital string maximization
Language: python

Solution 1 (runtime: 234 ms):
n = int(input())

for x in range(n):

s= list(map(int, input()))

g = len(s)

for i in range(1,g):

for j in range(0, s[i]):

if(i-j-1 < 0):

break

if(s[i-j]-1 > s[i-j-1]):

temp = s[i-j-1]

s[i-j-1] = s[i-j]-1

s[i-j] = temp

else:

break

result = ''.join(map(str,s))

print(result)

Solution 2 (runtime: 156 ms):
for _ in range(int(input())):

a = input()

s = [int(digit) for digit in a]

n = len(s)

for i in range(n):

best_value = s[i]

best_idx = -1

for j in range(i + 1, min(i + 9, n)):

score = s[j]-(j - i)

if score>best_value:

best_value=score

best_idx=j



if best_idx != -1:

# print(best_value,s[best_idx],i,best_idx)

s[i], s[best_idx] = best_value, s[i]

for j in range(best_idx, i+1, -1):

s[j], s[j - 1] = s[j - 1], s[j]

# print(s)



print("".join(map(str, s)))
Problem: 2050D - Digital string maximization
Language: python

Solution 1 (runtime: 234 ms):
n = int(input())

for x in range(n):

s= list(map(int, input()))

g = len(s)

for i in range(1,g):

for j in range(0, s[i]):

if(i-j-1 < 0):

break

if(s[i-j]-1 > s[i-j-1]):

temp = s[i-j-1]

s[i-j-1] = s[i-j]-1

s[i-j] = temp

else:

break

result = ''.join(map(str,s))

print(result)

Solution 2 (runtime: 171 ms):
mod = 1e9 + 7

for _ in range(int(input())):

s = list(map(int,list(input())))

for i in range(len(s)-1):

mxi = -1

mx = 0

for j in range(1,10):

if i + j >= len(s):

break

if int(s[i+j]) - j - int(s[i]) > mx:

mx = int(s[i+j]) - j - int(s[i])

mxi = j

if mxi == -1:

continue



for jj in range(i+mxi,i,-1):

s[jj],s[jj-1] = s[jj-1],s[jj] - 1



print(''.join(list(map(str,s))))
Problem: 2050D - Digital string maximization
Language: python

Solution 1 (runtime: 234 ms):
n = int(input())

for x in range(n):

s= list(map(int, input()))

g = len(s)

for i in range(1,g):

for j in range(0, s[i]):

if(i-j-1 < 0):

break

if(s[i-j]-1 > s[i-j-1]):

temp = s[i-j-1]

s[i-j-1] = s[i-j]-1

s[i-j] = temp

else:

break

result = ''.join(map(str,s))

print(result)

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

def solve():

s = [int(i) for i in list(input())]

n = len(s)

for i in range(1,n,1):

for j in range(max(i-10, 0), i):

if s[i] - (i-j) > s[j]:

tmp = s[j:i]

s[j] = s[i] - (i-j)

s[j+1:i+1] = tmp

break

print("".join([str(i) for i in s]))

return

for i in range(t):

solve()
Problem: 2050D - Digital string maximization
Language: python

Solution 1 (runtime: 234 ms):
n = int(input())

for x in range(n):

s= list(map(int, input()))

g = len(s)

for i in range(1,g):

for j in range(0, s[i]):

if(i-j-1 < 0):

break

if(s[i-j]-1 > s[i-j-1]):

temp = s[i-j-1]

s[i-j-1] = s[i-j]-1

s[i-j] = temp

else:

break

result = ''.join(map(str,s))

print(result)

Solution 2 (runtime: 906 ms):
def solve():

s = list(input())

n = len(s)

while True:

changed = False

for i in range(1, n):

if s[i] != '0' and int(s[i]) - 1 > int(s[i-1]):

num = int(s[i])

s[i] = str(int(s[i-1]))

s[i-1] = str(num - 1)

changed = True

if not changed:

break

print("".join(s))



t = int(input())

for _ in range(t):

solve()
Problem: 2050D - Digital string maximization
Language: python

Solution 1 (runtime: 234 ms):
n = int(input())

for x in range(n):

s= list(map(int, input()))

g = len(s)

for i in range(1,g):

for j in range(0, s[i]):

if(i-j-1 < 0):

break

if(s[i-j]-1 > s[i-j-1]):

temp = s[i-j-1]

s[i-j-1] = s[i-j]-1

s[i-j] = temp

else:

break

result = ''.join(map(str,s))

print(result)

Solution 2 (runtime: 265 ms):
for _ in range(int(input())):

s = list(map(int,input()))

for i in range(9):

for j in range(len(s)-1):

if s[j] < s[j+1] - 1:

s[j],s[j+1] = s[j+1]-1,s[j]

print(*s,sep= '')
Problem: 2050D - Digital string maximization
Language: python

Solution 1 (runtime: 156 ms):
for _ in range(int(input())):

a = input()

s = [int(digit) for digit in a]

n = len(s)

for i in range(n):

best_value = s[i]

best_idx = -1

for j in range(i + 1, min(i + 9, n)):

score = s[j]-(j - i)

if score>best_value:

best_value=score

best_idx=j



if best_idx != -1:

# print(best_value,s[best_idx],i,best_idx)

s[i], s[best_idx] = best_value, s[i]

for j in range(best_idx, i+1, -1):

s[j], s[j - 1] = s[j - 1], s[j]

# print(s)



print("".join(map(str, s)))



Solution 2 (runtime: 171 ms):
mod = 1e9 + 7

for _ in range(int(input())):

s = list(map(int,list(input())))

for i in range(len(s)-1):

mxi = -1

mx = 0

for j in range(1,10):

if i + j >= len(s):

break

if int(s[i+j]) - j - int(s[i]) > mx:

mx = int(s[i+j]) - j - int(s[i])

mxi = j

if mxi == -1:

continue



for jj in range(i+mxi,i,-1):

s[jj],s[jj-1] = s[jj-1],s[jj] - 1



print(''.join(list(map(str,s))))
Problem: 2050D - Digital string maximization
Language: python

Solution 1 (runtime: 156 ms):
for _ in range(int(input())):

a = input()

s = [int(digit) for digit in a]

n = len(s)

for i in range(n):

best_value = s[i]

best_idx = -1

for j in range(i + 1, min(i + 9, n)):

score = s[j]-(j - i)

if score>best_value:

best_value=score

best_idx=j



if best_idx != -1:

# print(best_value,s[best_idx],i,best_idx)

s[i], s[best_idx] = best_value, s[i]

for j in range(best_idx, i+1, -1):

s[j], s[j - 1] = s[j - 1], s[j]

# print(s)



print("".join(map(str, s)))



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

def solve():

s = [int(i) for i in list(input())]

n = len(s)

for i in range(1,n,1):

for j in range(max(i-10, 0), i):

if s[i] - (i-j) > s[j]:

tmp = s[j:i]

s[j] = s[i] - (i-j)

s[j+1:i+1] = tmp

break

print("".join([str(i) for i in s]))

return

for i in range(t):

solve()
Problem: 2050D - Digital string maximization
Language: python

Solution 1 (runtime: 156 ms):
for _ in range(int(input())):

a = input()

s = [int(digit) for digit in a]

n = len(s)

for i in range(n):

best_value = s[i]

best_idx = -1

for j in range(i + 1, min(i + 9, n)):

score = s[j]-(j - i)

if score>best_value:

best_value=score

best_idx=j



if best_idx != -1:

# print(best_value,s[best_idx],i,best_idx)

s[i], s[best_idx] = best_value, s[i]

for j in range(best_idx, i+1, -1):

s[j], s[j - 1] = s[j - 1], s[j]

# print(s)



print("".join(map(str, s)))



Solution 2 (runtime: 906 ms):
def solve():

s = list(input())

n = len(s)

while True:

changed = False

for i in range(1, n):

if s[i] != '0' and int(s[i]) - 1 > int(s[i-1]):

num = int(s[i])

s[i] = str(int(s[i-1]))

s[i-1] = str(num - 1)

changed = True

if not changed:

break

print("".join(s))



t = int(input())

for _ in range(t):

solve()
Problem: 2050D - Digital string maximization
Language: python

Solution 1 (runtime: 156 ms):
for _ in range(int(input())):

a = input()

s = [int(digit) for digit in a]

n = len(s)

for i in range(n):

best_value = s[i]

best_idx = -1

for j in range(i + 1, min(i + 9, n)):

score = s[j]-(j - i)

if score>best_value:

best_value=score

best_idx=j



if best_idx != -1:

# print(best_value,s[best_idx],i,best_idx)

s[i], s[best_idx] = best_value, s[i]

for j in range(best_idx, i+1, -1):

s[j], s[j - 1] = s[j - 1], s[j]

# print(s)



print("".join(map(str, s)))



Solution 2 (runtime: 265 ms):
for _ in range(int(input())):

s = list(map(int,input()))

for i in range(9):

for j in range(len(s)-1):

if s[j] < s[j+1] - 1:

s[j],s[j+1] = s[j+1]-1,s[j]

print(*s,sep= '')
Problem: 2050D - Digital string maximization
Language: python

Solution 1 (runtime: 171 ms):
mod = 1e9 + 7

for _ in range(int(input())):

s = list(map(int,list(input())))

for i in range(len(s)-1):

mxi = -1

mx = 0

for j in range(1,10):

if i + j >= len(s):

break

if int(s[i+j]) - j - int(s[i]) > mx:

mx = int(s[i+j]) - j - int(s[i])

mxi = j

if mxi == -1:

continue



for jj in range(i+mxi,i,-1):

s[jj],s[jj-1] = s[jj-1],s[jj] - 1



print(''.join(list(map(str,s))))

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

def solve():

s = [int(i) for i in list(input())]

n = len(s)

for i in range(1,n,1):

for j in range(max(i-10, 0), i):

if s[i] - (i-j) > s[j]:

tmp = s[j:i]

s[j] = s[i] - (i-j)

s[j+1:i+1] = tmp

break

print("".join([str(i) for i in s]))

return

for i in range(t):

solve()
Problem: 2050D - Digital string maximization
Language: python

Solution 1 (runtime: 171 ms):
mod = 1e9 + 7

for _ in range(int(input())):

s = list(map(int,list(input())))

for i in range(len(s)-1):

mxi = -1

mx = 0

for j in range(1,10):

if i + j >= len(s):

break

if int(s[i+j]) - j - int(s[i]) > mx:

mx = int(s[i+j]) - j - int(s[i])

mxi = j

if mxi == -1:

continue



for jj in range(i+mxi,i,-1):

s[jj],s[jj-1] = s[jj-1],s[jj] - 1



print(''.join(list(map(str,s))))

Solution 2 (runtime: 906 ms):
def solve():

s = list(input())

n = len(s)

while True:

changed = False

for i in range(1, n):

if s[i] != '0' and int(s[i]) - 1 > int(s[i-1]):

num = int(s[i])

s[i] = str(int(s[i-1]))

s[i-1] = str(num - 1)

changed = True

if not changed:

break

print("".join(s))



t = int(input())

for _ in range(t):

solve()
Problem: 2050D - Digital string maximization
Language: python

Solution 1 (runtime: 171 ms):
mod = 1e9 + 7

for _ in range(int(input())):

s = list(map(int,list(input())))

for i in range(len(s)-1):

mxi = -1

mx = 0

for j in range(1,10):

if i + j >= len(s):

break

if int(s[i+j]) - j - int(s[i]) > mx:

mx = int(s[i+j]) - j - int(s[i])

mxi = j

if mxi == -1:

continue



for jj in range(i+mxi,i,-1):

s[jj],s[jj-1] = s[jj-1],s[jj] - 1



print(''.join(list(map(str,s))))

Solution 2 (runtime: 265 ms):
for _ in range(int(input())):

s = list(map(int,input()))

for i in range(9):

for j in range(len(s)-1):

if s[j] < s[j+1] - 1:

s[j],s[j+1] = s[j+1]-1,s[j]

print(*s,sep= '')
Problem: 2050D - Digital string maximization
Language: python

Solution 1 (runtime: 218 ms):
t =  int(input())

def solve():

s = [int(i) for i in list(input())]

n = len(s)

for i in range(1,n,1):

for j in range(max(i-10, 0), i):

if s[i] - (i-j) > s[j]:

tmp = s[j:i]

s[j] = s[i] - (i-j)

s[j+1:i+1] = tmp

break

print("".join([str(i) for i in s]))

return

for i in range(t):

solve()

Solution 2 (runtime: 906 ms):
def solve():

s = list(input())

n = len(s)

while True:

changed = False

for i in range(1, n):

if s[i] != '0' and int(s[i]) - 1 > int(s[i-1]):

num = int(s[i])

s[i] = str(int(s[i-1]))

s[i-1] = str(num - 1)

changed = True

if not changed:

break

print("".join(s))



t = int(input())

for _ in range(t):

solve()
Problem: 2050D - Digital string maximization
Language: python

Solution 1 (runtime: 218 ms):
t =  int(input())

def solve():

s = [int(i) for i in list(input())]

n = len(s)

for i in range(1,n,1):

for j in range(max(i-10, 0), i):

if s[i] - (i-j) > s[j]:

tmp = s[j:i]

s[j] = s[i] - (i-j)

s[j+1:i+1] = tmp

break

print("".join([str(i) for i in s]))

return

for i in range(t):

solve()

Solution 2 (runtime: 265 ms):
for _ in range(int(input())):

s = list(map(int,input()))

for i in range(9):

for j in range(len(s)-1):

if s[j] < s[j+1] - 1:

s[j],s[j+1] = s[j+1]-1,s[j]

print(*s,sep= '')
Problem: 2050D - Digital string maximization
Language: python

Solution 1 (runtime: 906 ms):
def solve():

s = list(input())

n = len(s)

while True:

changed = False

for i in range(1, n):

if s[i] != '0' and int(s[i]) - 1 > int(s[i-1]):

num = int(s[i])

s[i] = str(int(s[i-1]))

s[i-1] = str(num - 1)

changed = True

if not changed:

break

print("".join(s))



t = int(input())

for _ in range(t):

solve()





Solution 2 (runtime: 265 ms):
for _ in range(int(input())):

s = list(map(int,input()))

for i in range(9):

for j in range(len(s)-1):

if s[j] < s[j+1] - 1:

s[j],s[j+1] = s[j+1]-1,s[j]

print(*s,sep= '')
Problem: 2050E - Three Strings
Language: python

Solution 1 (runtime: 265 ms):
def solve(a, b, c):
    n, m = len(a), len(b)
    
    dp = [[float('inf')] * (m + 1) for _ in range(n + 1)]
    dp[0][0] = 0
    
    for x in range(n + 1):
        for y in range(m + 1):
            if x < n:
                dp[x + 1][y] = min(dp[x + 1][y], dp[x][y] + int(a[x] != c[x + y]))
            if y < m:
                dp[x][y + 1] = min(dp[x][y + 1], dp[x][y] + int(b[y] != c[x + y]))

    return dp[n][m]

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


Solution 2 (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])
Problem: 2050E - Three Strings
Language: python

Solution 1 (runtime: 265 ms):
def solve(a, b, c):
    n, m = len(a), len(b)
    
    dp = [[float('inf')] * (m + 1) for _ in range(n + 1)]
    dp[0][0] = 0
    
    for x in range(n + 1):
        for y in range(m + 1):
            if x < n:
                dp[x + 1][y] = min(dp[x + 1][y], dp[x][y] + int(a[x] != c[x + y]))
            if y < m:
                dp[x][y + 1] = min(dp[x][y + 1], dp[x][y] + int(b[y] != c[x + y]))

    return dp[n][m]

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


Solution 2 (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))
Problem: 2050E - Three Strings
Language: python

Solution 1 (runtime: 265 ms):
def solve(a, b, c):
    n, m = len(a), len(b)
    
    dp = [[float('inf')] * (m + 1) for _ in range(n + 1)]
    dp[0][0] = 0
    
    for x in range(n + 1):
        for y in range(m + 1):
            if x < n:
                dp[x + 1][y] = min(dp[x + 1][y], dp[x][y] + int(a[x] != c[x + y]))
            if y < m:
                dp[x][y + 1] = min(dp[x][y + 1], dp[x][y] + int(b[y] != c[x + y]))

    return dp[n][m]

for _ in range(int(input())):
    a, b, c = input(), input(), input()
    print(solve(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: 265 ms):
def solve(a, b, c):
    n, m = len(a), len(b)
    
    dp = [[float('inf')] * (m + 1) for _ in range(n + 1)]
    dp[0][0] = 0
    
    for x in range(n + 1):
        for y in range(m + 1):
            if x < n:
                dp[x + 1][y] = min(dp[x + 1][y], dp[x][y] + int(a[x] != c[x + y]))
            if y < m:
                dp[x][y + 1] = min(dp[x][y + 1], dp[x][y] + int(b[y] != c[x + y]))

    return dp[n][m]

for _ in range(int(input())):
    a, b, c = input(), input(), input()
    print(solve(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])
Problem: 2050E - Three Strings
Language: python

Solution 1 (runtime: 265 ms):
def solve(a, b, c):
    n, m = len(a), len(b)
    
    dp = [[float('inf')] * (m + 1) for _ in range(n + 1)]
    dp[0][0] = 0
    
    for x in range(n + 1):
        for y in range(m + 1):
            if x < n:
                dp[x + 1][y] = min(dp[x + 1][y], dp[x][y] + int(a[x] != c[x + y]))
            if y < m:
                dp[x][y + 1] = min(dp[x][y + 1], dp[x][y] + int(b[y] != c[x + y]))

    return dp[n][m]

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


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