Guest User

Untitled

a guest
Jan 16th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.87 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.InputStreamReader;
  3.  
  4. public class EditDistance {
  5.  
  6. static char str1[], str2[];
  7. static int l1, l2;
  8. static int dp[][];
  9.  
  10. public static void main(String[] args) throws Exception {
  11.  
  12. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  13. int n = Integer.parseInt(br.readLine());
  14. while (n-- > 0) {
  15. str1 = br.readLine().toCharArray();
  16. str2 = br.readLine().toCharArray();
  17.  
  18. l1 = str1.length;
  19. l2 = str2.length;
  20. dp = new int[str1.length][str2.length];
  21.  
  22. System.out.println(solve(0, 0));
  23. }
  24. }
  25.  
  26. public static int solve(int i, int j) {
  27. if (i == l1) return (l2-j);
  28. if (j == l2) return (l1-i);
  29. if (dp[i][j] != 0)
  30. return dp[i][j];
  31. if (str1[i] == str2[j])
  32. return dp[i][j] = solve(i + 1, j + 1);
  33. return dp[i][j] = 1 + Math.min(Math.min(solve(i + 1, j), solve(i, j + 1)), solve(i + 1, j + 1));
  34. }
  35. }
Add Comment
Please, Sign In to add comment