Problem: 2050E - Three Strings
Language: java

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

public class Q1 {
    public static long solve(long n, HashMap<Long, Long> map) {
        if (n <= 3) {
            return 1;
        }

        if (map.containsKey(n)) {
            return map.get(n);
        }

        long count = solve(n / 4, map) + solve(n / 4, map);
        map.put(n, count);
        return map.get(n);
    }

    public static long gcd(long a, long b) {
        if (a == 0)
            return b;
        if (b == 0)
            return a;

        // Base case
        if (a == b)
            return a;

        // a is greater
        if (a > b) {
            if (a % b == 0)
                return b;
            return gcd(a - b, b);
        }
        if (b % a == 0)
            return a;
        return gcd(a, b - a);
    }

    public static int solve(int ai, int ci, String a, String b, String c, int dp[][]) {
        int bi = ci - ai;
        if (ai == a.length()) {
            String s1 = b.substring(bi);
            String s2 = c.substring(ci);
            if (s1.equals(s2)) {
                return 0;
            }
            int cnt = 0;
            for (int i = 0; i < s1.length(); i++) {
                if (s1.charAt(i) != s2.charAt(i)) {
                    cnt++;
                }
            }

            return cnt;
        }

        if (bi == b.length()) {
            String s1 = a.substring(ai);
            String s2 = c.substring(ci);
            if (s1.equals(s2)) {
                return 0;
            }
            int cnt = 0;
            for (int i = 0; i < s1.length(); i++) {
                if (s1.charAt(i) != s2.charAt(i)) {
                    cnt++;
                }
            }

            return cnt;
        }

        if (dp[ai][ci] != -1) {
            return dp[ai][ci];
        }

        int min = Integer.MAX_VALUE;
        if (a.charAt(ai) == c.charAt(ci)) {
            min = Math.min(solve(ai + 1, ci + 1, a, b, c, dp), min);
        }

        if (b.charAt(bi) == c.charAt(ci)) {
            min = Math.min(solve(ai, ci + 1, a, b, c, dp), min);
        }

        if (a.charAt(ai) != c.charAt(ci) || b.charAt(bi) != c.charAt(ci)) {
            min = Math.min(1 + Math.min(solve(ai + 1, ci + 1, a, b, c, dp), solve(ai, ci + 1, a, b, c, dp)), min);
        }

        return dp[ai][ci] = min;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        while (t-- > 0) {
            String a = sc.next();
            String b = sc.next();
            String c = sc.next();
            int dp[][] = new int[a.length()][c.length()];
            for (int row[] : dp) {
                Arrays.fill(row, -1);
            }
            System.out.println(solve(0, 0, a, b, c, dp));
        }
    }
}

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