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: 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;
        }
    }
 
}