Problem: 2050E - Three Strings
Language: java

Solution 1 (runtime: 343 ms):
import java.util.Arrays;
import java.util.Scanner;

public class ThreeStrings {
    public static void main(String[] args) {
        Scanner sc= new Scanner(System.in);
        int t= sc.nextInt();
        for (int i = 0; i < t; i++) {
            String a = sc.next();
            String b = sc.next();
            String c = sc.next();

            int lenA = a.length();
            int lenB = b.length();
            int lenC = c.length();

            long dp[][] = new long[lenA + 10][lenB + 10];

            for(long [] x : dp ) Arrays.fill( x , -1 );

            System.out.println(rec( 0 , 0 , dp , lenA , lenB , a , b, c));
        }
    }

    static long rec ( int x , int y , long dp[][] , int lenA , int lenB , String a , String b, String c){

        if ( x == lenA ){
            long add = 0;
            for(int i = y ; i < lenB ;i++){
                if ( b.charAt(i) != c.charAt( x + i )) add ++ ;
            }
            return add ;
        }
        if ( y == lenB ){
            long add = 0;
            for(int i = x ; i < lenA ;i++){
                if ( a.charAt(i) != c.charAt( i + y )) add ++ ;
            }
            return add ;
        }
        if ( dp[x][y] != -1) return dp[x][y];
        long ans1 = (a.charAt( x ) == c.charAt( x + y ) ? 0 : 1 ) + rec( x + 1 , y , dp , lenA , lenB , a, b, c);
        long ans2 = (b.charAt( y ) == c.charAt( x + y ) ? 0 : 1 ) + rec( x  , y + 1 , dp , lenA , lenB , a, b, c);

        return dp[x][y] = Math.min(ans1 , ans2);
    }
}


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