Advertisement
Guest User

Untitled

a guest
Jan 17th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.26 KB | None | 0 0
  1. package levenshtein;
  2.  
  3. import javax.swing.JOptionPane;
  4.  
  5. public class Levenshtein {
  6.  
  7. private String s1 = new String();
  8. private String s2 = new String();
  9. private int n;
  10.  
  11. // métodos
  12. public void set(){
  13. s1 = JOptionPane.showInputDialog("Digite o texto 1:");
  14. s2 = JOptionPane.showInputDialog("Digite o texto 2:");
  15. }
  16.  
  17. public void print() {
  18. n=distancia();
  19. JOptionPane.showMessageDialog(null, "Número mínimo de operações: " + n);
  20. }
  21.  
  22. public int distancia() {
  23. int n1 = s1.length();
  24. int n2 = s2.length();
  25. int i, j;
  26. int t[] = new int[n2 + 1];
  27. for (i = 0; i <= n2; i++) {
  28. t[i] = i;
  29. }
  30. for (i = 0; i < n1; i++) {
  31. t[0] = i + 1;
  32. int canto = i;
  33. for (j = 0; j < n2; j++) {
  34. int upper = t[j + 1];
  35. if (s1.charAt(i) == s2.charAt(j)) {
  36. t[j + 1] = canto;
  37. } else {
  38. t[j + 1] = Math.min(t[j], Math.min(upper, canto)) + 1;
  39. }
  40. canto = upper;
  41. }
  42. }
  43. return t[n2];
  44. }
  45.  
  46. public static void main(String[] args) {
  47. Levenshtein v = new Levenshtein();
  48. v.set();
  49. v.print();
  50. }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement