Problem: 2050E - Three Strings
Language: java

Solution 1 (runtime: 280 ms):
import java.util.*;
import java.io.*;

public class E {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter pw = new PrintWriter(System.out);

        int tc = Integer.parseInt(br.readLine());

        while (tc-- > 0) {
            String s1 = br.readLine();
            String s2 = br.readLine();
            String s3 = br.readLine();

            int[][] dp = new int[s2.length()+1][s1.length()+1];

            for (int c = 1; c < dp[0].length; c++) {
                dp[0][c] = dp[0][c-1];                
                if (s3.charAt(c-1) != s1.charAt(c-1)) {
                    dp[0][c]++;
                }
            }

            for (int r = 1; r < dp.length; r++) {
                dp[r][0] = dp[r-1][0];
                if (s3.charAt(r-1) != s2.charAt(r-1)) {
                    dp[r][0]++;
                }
            }

            for (int r = 1; r < dp.length; r++) { // r = s2 used
                for (int c = 1; c < dp[0].length; c++) { // c = s1 used
                    int s1add = s3.charAt(r+c-1) == s1.charAt(c-1) ? 0 : 1;
                    int s2add = s3.charAt(r+c-1) == s2.charAt(r-1) ? 0 : 1;

                    dp[r][c] = Math.min(dp[r-1][c] + s2add, dp[r][c-1] + s1add);
                }
            }
            
            pw.println(dp[s2.length()][s1.length()]);
        }

        pw.close();
    }
}


Solution 2 (runtime: 280 ms):
import java.util.*;

import java.lang.*;

import java.io.*;



public class Codeforces {

    public static void main(String[] args){

        FastReader fr = new FastReader();

        int tc = fr.nextInt();



        while (tc-- > 0) {

            solve(fr);

        }

    }





    private static void solve(FastReader fr) {

        String a = fr.next();

        String b = fr.next();

        String c = fr.next();

        int[][] dp = new int[a.length()+1][b.length()+1];



        for(int[] row : dp) Arrays.fill(row, -1);



        System.out.println(solve(a, b, c, 0, 0, 0, dp));

    }



    static int solve(String a, String b, String c, int i, int j, int k, int[][] dp) {

        if(k == c.length()) return 0;

        if(i > a.length() || j > b.length()) return (int)(1e7);

        boolean isAMatch = false;

        boolean isBMatch = false;



        if(dp[i][j] != -1) return dp[i][j];



        if(i < a.length() && a.charAt(i) == c.charAt(k)){

            isAMatch = true;

        }



        if(j < b.length() && b.charAt(j) == c.charAt(k)){

            isBMatch = true;

        }



        int ans1 = solve(a, b, c, i+1, j, k+1, dp) + (isAMatch ? 0 : 1);

        int ans2 = solve(a, b, c, i, j + 1, k+1, dp) + (isBMatch ? 0 : 1);



        return dp[i][j] = Math.min(ans1, ans2);

    }





    static int[] readIntArray(FastReader fr, int n){

        int[] arr = new int[n];



        for(int i = 0; i < n; i++){

            arr[i] = fr.nextInt();

        }



        return arr;

    }



    static class FastReader {

        BufferedReader br;

        StringTokenizer st;



        public FastReader() {

            br = new BufferedReader(

                    new InputStreamReader(System.in));

        }



        String next() {

            while (st == null || !st.hasMoreElements()) {

                try {

                    st = new StringTokenizer(br.readLine());

                } catch (IOException e) {

                    e.printStackTrace();

                }

            }

            return st.nextToken();

        }



        int nextInt() {

            return Integer.parseInt(next());

        }



        long nextLong() {

            return Long.parseLong(next());

        }



        double nextDouble() {

            return Double.parseDouble(next());

        }



        String nextLine() {

            String str = "";

            try {

                if (st.hasMoreTokens()) {

                    str = st.nextToken("\n");

                } else {

                    str = br.readLine();

                }

            } catch (IOException e) {

                e.printStackTrace();

            }

            return str;

        }

    }



}