import java.util.*; public class Gotoh1 { public static int C[][]; // Min cost (overall) public static int D[][]; // Min cost that ends in delete x_i public static int I[][]; // Min cost that ends in insert y_j public static int g = 20; // Gap opening cost public static int h = 2; // Gap continuation cost public static int w( char a, char b ) { if ( a == b ) return 0; else return 3; } public static int solveMinAlignCost(String x, String y) { int i, j; int m, n; m = x.length(); n = y.length(); C[0][0] = 0; for (j = 1; j <= n ; j++) { C[0][j] = g + j*h; // C(0,j) = gap(j) = g + j*h D[0][j] = C[0][j] + g; // D(0,j) = C(0,j) + g (def) } for (i = 1; i <= m; i++) { C[i][0] = g +i*h; // C(i,0) = gap(i) = g + i*h I[i][0] = C[i][0] + g; // I(i,0) = C(i,0) + g (def) for (j = 1; j <= n; j++) { D[i][j] = min2 ( D[i-1][j]+h , C[i-1][j]+g+h ); I[i][j] = min2 ( I[i][j-1]+h , C[i][j-1]+g+h ); C[i][j] = min3 ( D[i][j], I[i][j], C[i-1][j-1] + w( x.charAt(i-1), y.charAt(j-1) ) ); } } return C[m][n]; } public static int min2( int x, int y ) { return ( x < y ? x : y ); } public static int min3( int x, int y, int z ) { int m = ( x < y ? x : y ); return ( m < z ? m : z ); } public static void main(String[] args) { Scanner in = new Scanner(System.in); String x; String y; int i, j, r, minCost; System.out.print("x = "); x = in.next(); System.out.print("y = "); y = in.next(); /* x = "AAAA"; y = "AAA"; */ C = new int[x.length()+1][y.length()+1]; D = new int[x.length()+1][y.length()+1]; I = new int[x.length()+1][y.length()+1]; minCost = solveMinAlignCost(x, y); System.out.println("x = " + x); System.out.println("y = " + y); System.out.println("Min. Align. Cost = " + minCost ); System.out.println(); System.out.println(); System.out.println("C[][]:"); System.out.print(" "); for (j = 0; j < y.length()+1; j++) System.out.print(" " + j); System.out.println(); System.out.println("=================================="); for (i = 0; i < x.length()+1; i++) { if ( i < 10 ) System.out.print(" "); System.out.print(" " + i + " "); for (j = 0; j < y.length()+1; j++) System.out.print(" " + C[i][j]); System.out.println(); } } }