/* -------------------------------------------------------- This program is derived from Gotoh1f_lin_space.java I used double type cost -------------------------------------------------------- */ import java.util.*; public class Myers { public static double CC[]; // Min cost (overall) public static double DD[]; // Min cost that ends in delete x_i public static double e, c, s, t; public static double g = 2; // Gap opening cost public static double h = .5; // Gap continuation cost public static double w( char a, char b ) { if ( a == b ) return 0; else return 1; } public static double solveMinAlignCost(String x, String y ) { int i, j; int m, n; m = x.length(); n = y.length(); CC[0] = 0; t = g; for (j = 1; j <= n ; j++) { CC[j] = t = t + h; // C(0,j) = gap(j) = g + j*h DD[j] = t + g; // D(0,j) = C(0,j) + g (def) } t = g; for (i = 1; i <= m; i++) { s = CC[0]; CC[0] = c = t = t + h; // C(i,0) = gap(i) = g + i*h e = t + g; // I(i,0) = C(i,0) + g (def) for (j = 1; j <= n; j++) { e = min2 ( e+h, c+g+h ); DD[j] = min2 ( DD[j]+h, CC[j] + g + h ); // DD[1][j] = min2 ( DD[0][j]+h , CC[0][j]+g+h ); c = min3 ( DD[j], e, s + w( x.charAt(i-1), y.charAt(j-1) ) ); s = CC[j]; CC[j] = c; // CC[1][j] = min3 ( DD[1][j], II[1][j], // CC[0][j-1] + w( x.charAt(i-1), y.charAt(j-1) ) ); } } return CC[n]; } public static double min2( double x, double y ) { return ( x < y ? x : y ); } public static double min3( double x, double y, double z ) { double 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; double minCost; System.out.print("x = "); x = in.next(); System.out.print("y = "); y = in.next(); /* x = "AAAA"; y = "AAA"; */ CC = new double[y.length()+1]; // Linear space ! DD = new double[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(); } }