Advertisement
bool_bool

SPOJ EDIST.java

Jul 9th, 2019
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.92 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Main {
  4.  
  5. public static void main(String[] args) {
  6.  
  7. Scanner sc=new Scanner(System.in);
  8. int t;
  9. t=sc.nextInt();
  10. while(t>0){
  11. t--;
  12. String a=sc.next();
  13. String b=sc.next();
  14. int n=a.length();
  15. int m=b.length();
  16. int [][]dp=new int[m+1][n+1];
  17.  
  18. for(int i=0;i<=m;i++)
  19. dp[i][0]=i;
  20. for(int i=0;i<=n;i++)
  21. dp[0][i]=i;
  22. for(int i=0;i<m;i++)
  23. for(int j=0;j<n;j++){
  24. if(b.charAt(i)==a.charAt(j))
  25. dp[i+1][j+1]=dp[i][j];
  26. else{
  27. dp[i+1][j+1]=1+Integer.min(dp[i+1][j],Integer.min(dp[i][j],dp[i][j+1]));
  28. }
  29. }
  30.  
  31.  
  32. System.out.println(dp[m][n]);
  33. }
  34.  
  35. }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement