/* -------------------------------------------------------- This program is derived from Myers.java I added solveRevMinAlignCost(String x, String y ) -------------------------------------------------------- */ import java.util.*; public class Myers2 { 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 void DIFF( String A, String B ) { diff( A, B, t_begin, t_end ); } public static void DIFF( String A, String B, double t_begin, double t_end ) { int m = A.length(); int n = B.length(); if ( n == 0 ) { if ( m > 0 ) System.out.println("delete " + A); return; } else if ( m == 0 ) { if ( n > 0 ) System.out.println("insert " + B); return; } else if ( m == 1 ) { } else { i_star = m/2; solveMinAlignCost(String x, String y ); // get CC[] and DD[] solve_Rev_MinAlignCost(String x, String y ); // get RR[] and SS[] if ( CC[0] + RR[n] < DD[0] + SS[n] - g ) { min_value = CC[0] + RR[n]; action = 'r'; } else { min_value = DD[0] + SS[0] - g; action = 'd'; } min_j = 0; for ( j = 1; j <= n; j++ ) { if ( CC[j] + RR[n-j] < min_value ) { min_value = CC[j] + RR[n-j]; action = 'r'; min_j = j; } if ( DD[j] + SS[n-j] - g < min_value ) { min_value = DD[j] + SS[n-j] - g; action = 'd'; min_j = j; } } if ( action == 'r' ) { diff ( x.substring(0, i_star), y.substring(0, min_j), tb, g); diff ( x.substring(i_star, m), y.substring(min_j, n), g, te); } else { diff ( x.substring(0, i_star-1), y.substring(0, min_j), tb, 0); System.out.println("Delete " + a.charAt(i_star) + a.charAt(i_star+1) ); diff ( x.substring(i_star+1, m), y.substring(min_j, n), 0, te); } } } 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 String reverse(String x) { StringBuffer y = new StringBuffer( x.length() ); for ( int i = x.length()-1; i >= 0; i-- ) y.append( x.charAt(i) ); return y.toString(); } public static double solve_Rev_MinAlignCost(String a, String b ) { String x, y; x = reverse(a); y = reverse(b); 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(); } }