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: 156 ms):
import java.util.*;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigInteger;

public class E_Three{
    public static void main(String[] args) throws IOException{
        InputReader sc= new InputReader (System.in);
        int t=sc.readInt();
        while(t-->0){
            
          String a=sc.readString();
          String b=sc.readString();
          String c=sc.readString();
          int max=(int)1e9;
          int n=a.length();
          int m=b.length();
          int[][] dp=new int[n+1][m+1];
          for(int i=0;i<=n;i++){
            for(int j=0;j<=m;j++){
                dp[i][j]=max;
            }
          }
          dp[0][0]=0;
          for(int i=0;i<n;i++){
            dp[i+1][0]=dp[i][0]+(a.charAt(i)==c.charAt(i)?0:1);
          }
          for(int j=0;j<m;j++){
            dp[0][j+1]=dp[0][j]+(b.charAt(j)==c.charAt(j)?0:1);
          }
          for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                dp[i][j]=Math.min(dp[i-1][j]+(a.charAt(i-1)==c.charAt(i+j-1)?0:1),dp[i][j-1]+(b.charAt(j-1)==c.charAt(i+j-1)?0:1));
            }
          }
          System.out.println(dp[n][m]);



          
            

           
            
            



            
            
            
            

            
            
            
           
            
           
           

            

           


           
           
        


    }

}
public static int countOccurence(String s1,String s2){
    int n=s1.length();
    int m=s2.length();
    int [][] dp=new int[n+1][m+1];
    for(int i=0;i<=m;i++){
        dp[0][i]=0;
    }
    for(int i=0;i<=n;i++){
        dp[i][0]=1;
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            if(s1.charAt(i-1)==s2.charAt(j-1)){
                dp[i][j]=dp[i-1][j-1]+dp[i-1][j];
            }
            else{
                dp[i][j]=dp[i-1][j];
            }
        }
    }
    return dp[n][m];
}
public static int solve(int[] a,int x){
    int [] count=new int[a.length+1];
    int n=count.length;
    for(int i:a){
        if(i<count.length){
            count[i]++;
        }
    }
    int result=0;
    while(count[result]!=0){
        if(result+x<n){
            count[result+x]+=count[result]-1;
        }
        result++;
    }
    return result;
}

public static BigInteger factorial (int n){
    BigInteger result=BigInteger.ONE;
    for(int i=1;i<=n;i++){
        result=result.multiply(BigInteger.valueOf(i));
    }
    return result;
}
public static int [] count(int[] p,int n,String s){
    boolean [] visited=new boolean[n+1];
    int [] zeroCount=new int[n+1];
    for(int i=1;i<=n;i++){
        if(!visited[i]){
            List<Integer> cycle=new ArrayList<>();
            int curr=i;
            while(!visited[curr]){
                visited[curr]=true;
                cycle.add(curr);
                curr=p[curr];
            }
            int count=0;
            for(int ind:cycle){
                if(s.charAt(ind-1)=='0'){
                    count++;
                }
            }
            for(int ind:cycle){
                zeroCount[ind]=count;
            }
        }
        
    }
    return zeroCount;
}
public static int gcd(int a,int b){
    if(b==0){
        return a;
    }
    return gcd(b,a%b);
}
public static boolean isPrime(int n){
    boolean isPrime=true;
    for(int i=2;i*i<=n;i++){
        if(n%i==0){
            isPrime=false;
            break;
        }
    }
    return isPrime;
}
   
static final class InputReader {
        private final InputStream stream;
        private final byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
 
        public InputReader(InputStream stream) {
            this.stream = stream;
        }
 
        private int read() throws IOException {
            if (curChar >= numChars) {
                curChar = 0;
                numChars = stream.read(buf);
                if (numChars <= 0) {
                    return -1;
                }
            }
            return buf[curChar++];
        }
 
        public final int readInt() throws IOException {
            return (int)readLong();
        }
 
        public final long readLong() throws IOException {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
                if (c == -1) throw new IOException();
            }
            boolean negative = false;
            if (c == '-') {
                negative = true;
                c = read();
            }
            long res = 0;
            do {
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return negative ? -res : res;
        }
 
        public final String readString() throws IOException {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            StringBuilder res = new StringBuilder();
            do {
                res.append((char)c);
                c = read();
            } while (!isSpaceChar(c));
            return res.toString();
        }
public final char readChar() throws IOException {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            return (char) c;
        }
 
        private boolean isSpaceChar(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }
    }
 
}