Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.18 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Scanner;
  3. import java.math.*;
  4.  
  5. class MinEditDistance
  6. {
  7. public static void main(String args[])
  8. {
  9. Scanner s = new Scanner(System.in);
  10.  
  11. // String input1 = s.nextLine();
  12. // String input2 = s.nextLine();
  13.  
  14. String input1 = "abcdefg";
  15. String input2 = "abcdegg";
  16.  
  17. // String input1 = "intention";
  18. // String input2 = "execution";
  19.  
  20. // String input1 = "naruto's son";
  21. // String input2 = "boruto's dad";
  22.  
  23. // String input1 = "bugs bunny";
  24. // String input2 = "big chungus";
  25.  
  26. // String input1 = "kumakain";
  27. // String input2 = "kumain";
  28.  
  29. // String input1 = "levinstien";
  30. // String input2 = "levenshtein";
  31.  
  32. // String input1 = "leviathan";
  33. // String input2 = "levenshtein";
  34.  
  35. // String input1 = "ATGCATCCCATGAC";
  36. // String input2 = "TCTATATCCGT";
  37.  
  38. // String input1 = "AGGCTATCACCTGACCTCCAGGCCGATGCCCACCTGG";
  39. // String input2 = "TAGCTATCACGACCGCGGTCGATTTGCCCGACGGTCC";
  40.  
  41. int m = input1.length();
  42. int n = input2.length();
  43.  
  44. //initialize table
  45. int DP[][] = new int[m+1][n+1];
  46. ArrayList<Character> BT = new ArrayList<Character>();
  47. ArrayList<Character> BT2 = new ArrayList<Character>();
  48.  
  49. // populate table
  50. for (int i=0; i<=m; i++)
  51. {
  52. for (int j=0; j<=n; j++)
  53. {
  54. if (i==0)
  55. DP[i][j] = j;
  56.  
  57. else if (j==0)
  58. DP[i][j] = i;
  59.  
  60. else if (input1.charAt(i-1) == input2.charAt(j-1))
  61. {
  62. DP[i][j] = Math.min(DP[i][j-1]+1, Math.min(DP[i-1][j]+1, DP[i-1][j-1]+0)); //match
  63. }
  64.  
  65. else
  66. {
  67. DP[i][j] = Math.min(DP[i][j-1]+1, Math.min(DP[i-1][j]+1, DP[i-1][j-1]+2)); //mismatch
  68. }
  69. }
  70. }
  71.  
  72. //print table
  73. for (int j = 0; j < n+1; j++)
  74. {
  75. for (int i = 0; i < m+1; i++)
  76. System.out.print(DP[i][j]+"\t");
  77.  
  78. System.out.println();
  79. }
  80.  
  81. System.out.println("\n\nDISTANCE: "+DP[m][n]);
  82.  
  83. int i = m, j = n;
  84. while (i != 0 && j != 0)
  85. {
  86. if (i>0 && j>0)
  87. {
  88. //both input are not at the end
  89. if (DP[i-1][j-1] <= DP[i-1][j] && DP[i-1][j-1] <= DP[i][j-1])
  90. {
  91. System.out.println("went to "+DP[i-1][j-1]);
  92. System.out.println("\\");
  93. BT.add('M');
  94. i--;
  95. j--;
  96. }
  97.  
  98. else if (DP[i-1][j] >= DP[i][j-1])
  99. {
  100. System.out.println("went to "+DP[i][j-1]);
  101. System.out.println("-");
  102. BT.add('I');
  103. i--;
  104. }
  105.  
  106. else if (DP[i-1][j] <= DP[i][j-1])
  107. {
  108. System.out.println("went to "+DP[i-1][j]);
  109. System.out.println("|");
  110. BT.add('D');
  111. j--;
  112. }
  113. }
  114.  
  115. else if (i==0)
  116. {
  117. // input m is at the end
  118. System.out.println("|");
  119. BT.add('D');
  120. j--;
  121. }
  122.  
  123. else
  124. { // input n is at the end
  125. System.out.println("-");
  126. BT.add('I');
  127. i--;
  128. }
  129. }
  130.  
  131. // System.out.println(BT);
  132.  
  133. System.out.println("\nalignment: ");
  134. for(int x = BT.size()-1; x >= 0; x--)
  135. System.out.print(BT.get(x) + " ");
  136.  
  137. // System.out.println("\nsteps (from top): ");
  138. // for(int x = BT.size()-1; x >= 0; x--)
  139. // System.out.print(BT2.get(x) + " ");
  140.  
  141. }
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement