Problem: 2050E - Three Strings
Language: java

Solution 1 (runtime: 624 ms):
import java.io.*;
import java.lang.reflect.Array;
import java.util.*;

public class MyClass {
    public static void main(String[] args) {
        FastScanner in = new FastScanner();
        PrintWriter out = new PrintWriter(System.out);
        int t = in.nextInt();
        while (t-- > 0)
            solve(in, out);
        out.close();
    }
    static long pow(long x,long n, long m) {
        if(n==0) return 1;
        else if(n%2==0) return pow((x*x)%m,n/2,m)%m;
        else return (x*pow((x*x)%m,n/2,m))%m;
    }

    static int gcd(int a,int b) {
        if(b==0) return a;
        else return gcd(b,a%b);
    }

    static int ans;
    static void findSum(int[][] a,int i, int j, int sum) {
        if(i>2 || j>2) return;
        findSum(a,i,j+1, a[i][j]+sum);
        findSum(a,i+1,j, a[i][j]+sum);
        if(i==2 && j==2) ans=Math.min(a[i][j]+sum, ans);
    }

    static char[] a,b,c;
    static int A[][];
    static int findMaxMatching(int i, int j, int k){
        if(i>=a.length){
            int total=0;
            for (int l = j; l < b.length; l++) {
                if(b[l]==c[k]){
                   total++;
                }
                k++;
            }
            return total;
        }
        if(j>=b.length){
            int total=0;
            for (int l = i; l < a.length; l++) {
                if(a[l]==c[k]){
                    total++;
                }
                k++;
            }
            return total;
        }
        if(A[i][j]>0) return A[i][j];
        int possibilityA = findMaxMatching(i+1,j,k+1);
        if(a[i]==c[k]){
            possibilityA++;
        }
        int possibilityB = findMaxMatching(i,j+1,k+1);
        if(b[j]==c[k]){
            possibilityB++;
        }
        A[i][j] = Math.max(possibilityA, possibilityB);
        return A[i][j];
    }
    static void solve(FastScanner in,PrintWriter out) {
        a=in.next().toCharArray();
        b=in.next().toCharArray();
        c=in.next().toCharArray();
        A=new int[a.length][b.length];
        int max = findMaxMatching(0,0,0);
        out.println(c.length-max);
    }

    static class pair<U extends Comparable<U>, V extends Comparable<V>> implements Comparable<pair<U, V>> {
        public U x;
        public V y;

        public pair(U x, V y) {
            this.x = x;
            this.y = y;
        }

        public int compareTo(pair<U, V> other) {
            int i = other.y.compareTo(y);
            if (i != 0) return i;
            return other.x.compareTo(x);
        }

        public String toString() {
            return x.toString() + " " + y.toString();
        }

        public boolean equals(Object obj) {
            if (this.getClass() != obj.getClass()) return false;
            pair<U, V> other = (pair<U, V>) obj;
            return x.equals(other.x) && y.equals(other.y);
        }

        public int hashCode() {
            return 31 * x.hashCode() + y.hashCode();
        }

    }

    static void sort(int[] a) {
        ArrayList<Integer> l=new ArrayList<>();
        for (int i:a) l.add(i);
        Collections.sort(l);
        for (int i=0; i<a.length; i++) a[i]=l.get(i);
    }
    static void sortR(int[] a) {
        ArrayList<Integer> l=new ArrayList<>();
        for (int i:a) l.add(i);
        Collections.sort(l,Collections.reverseOrder());
        for (int i=0; i<a.length; i++) a[i]=l.get(i);
    }

    static class FastScanner {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st=new StringTokenizer("");
        String next() {
            while (!st.hasMoreTokens())
                try {
                    st=new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }
        int[] readArray(int n) {
            int[] a=new int[n];
            for (int i=0; i<n; i++) a[i]=nextInt();
            return a;
        }
        long nextLong() {
            return Long.parseLong(next());
        }
    }
}

Solution 2 (runtime: 296 ms):
//package codeforcesques;
import java.util.*;
import java.io.*;
public class ThreeStrings {
    int mincount = Integer.MAX_VALUE;

    public static void main(String[] args) {
        FastReader fs = new FastReader();

        int t = fs.nextInt();

        while (t-- > 0) {
            String s1 = fs.nextLine();
            String s2 = fs.nextLine();
            String s3 = fs.nextLine();
            ThreeStrings d = new ThreeStrings();
            int[][] dp = new int[s1.length()][s2.length()];
            for (int i = 0; i < s1.length(); i++) {
                Arrays.fill(dp[i], -1);
            }
            System.out.println( s3.length()-rec(0, 0, 0, s1, s2, s3, dp));
        }
    }
    public static int rec(int n, int i, int j, String a, String b, String c, int[][] dp) {
        if (i == a.length()) {
            int val = 0;
            while (j < b.length()) {
                if (b.charAt(j) == c.charAt(n)) {
                    val++;
                }
                j++;
                n++;
            }
            return val;
        }
        if (j == b.length()) {
            int val = 0;
            while (i < a.length()) {
                if (a.charAt(i) == c.charAt(n)) {
                    val++;
                }
                i++;
                n++;
            }
            return val;
        }
        if (dp[i][j] != -1) {
            return dp[i][j];
        }
        int c1 = 0;
        int c2 = 0;
        if (a.charAt(i) == c.charAt(n)) {
            c1++;
        }
        if (b.charAt(j) == c.charAt(n)) {
            c2++;
        }
        int max1 = rec(n + 1, i + 1, j, a, b, c, dp) + c1;
        int max2 = rec(n + 1, i, j + 1, a, b, c, dp) + c2;
        dp[i][j] = Math.max(max1, max2);
        return dp[i][j];
    }
}
class FastReader {
    BufferedReader gt;
    StringTokenizer st;

    public FastReader() {
        gt = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(gt.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    String nextLine() {
        String str = "";
        try {
            str = gt.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return str;
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    int[] readIntArray(int n) {
        int[] res = new int[n];
        for (int i = 0; i < n; i++)
            res[i] = nextInt();
        return res;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    long[] readLongArray(int n) {
        long[] res = new long[n];
        for (int i = 0; i < n; i++)
            res[i] = nextLong();
        return res;
    }

    String[] readStringArray(int n) {
        String[] str = new String[n];
        for (int i = 0; i < n; i++)
            str[i] = nextLine();

        return str;
    }

    int getGCD(int a, int b) {
        if (b == 0)
            return a;
        else
            return getGCD(b, a % b);
    }
}