Problem: 2050E - Three Strings
Language: java

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


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