Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.38 KB | None | 0 0
  1. import java.util.*;
  2. import java.io.*;
  3. import java.text.*;
  4. import java.math.*;
  5. import static java.lang.System.*;
  6. import static java.lang.Integer.*;
  7. import static java.lang.Double.*;
  8. import static java.lang.Math.*;
  9.  
  10. public class EditDist
  11. {
  12.    
  13.     public void run() throws Exception
  14.     {
  15.         Scanner file = new Scanner(new File("EditDistance.dat"));
  16.        
  17.         int times = file.nextInt();
  18.         file.nextLine();
  19.        
  20.         for (int asdf = 0; asdf < times; asdf++)
  21.         {
  22.             String s1 = file.nextLine();
  23.             String s2 = file.nextLine();
  24.             int[][] dp = new int[s1.length()+1][s2.length()+1];
  25.             for(int i = 0; i < dp.length-1; i++)
  26.             {
  27.                 for(int j = 0; j < dp.length-1; j++)
  28.                 {
  29.                     if(s1.substring(i, i+1).equals(s2.substring(j, j+1))) dp[i+1][j+1] =dp[i][j]; //Im confused here
  30.                     else dp[i+1][j+1] = Math.min(dp[i][j]+1, Math.min(dp[i][j+1]+1, dp[i+1][j]+1));
  31.                 }
  32.             }
  33.             System.out.println(dp[s1.length()][s2.length()]);
  34.             //Question : will the answer always be in the bottom right cell bc you are bringing it down right?
  35.         }
  36.     }
  37.    
  38.     public static void main(String[] args) throws Exception
  39.     {
  40.        
  41.         new EditDist().run();
  42.         //data
  43.         /*
  44.         4
  45.         abcdefghijklmnopqrstuvxyz
  46.         zyxvutsrqponmlkjihgfedcba
  47.         Ral
  48.         The Ral
  49.         Steven
  50.         Steven
  51.         abcdefg
  52.         abdcegg
  53.         */
  54.         //output              //correct output
  55.         /*                     
  56.         24                    24
  57.         0                     4
  58.         0                     0
  59.         3                     3
  60.         */ 
  61.     }
  62.    
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement