fork download
  1. class Testy
  2. {
  3. public static void main(String[] args) throws java.io.IOException
  4. {
  5. String A = "babab";
  6. String B = "abaabbaaa";
  7.  
  8. int n = A.length();
  9. int m = B.length();
  10.  
  11. int[][] C = new int[n+1][m+1];
  12.  
  13. for (int i=0;i<n;i++)
  14. {
  15. for (int j=0;j<m;j++)
  16. {
  17. if (A.charAt(i) == B.charAt(j))
  18. C[i+1][j+1] = C[i][j] + 1;
  19. else
  20. C[i+1][j+1] = Math.max(C[i+1][j], C[i][j+1]);
  21. }
  22. }
  23.  
  24. for (int i=0;i<n+1;i++)
  25. {
  26. for (int j=0;j<m+1;j++)
  27. System.out.print(C[i][j]+" ");
  28. System.out.println();
  29. }
  30. }
  31. }
Success #stdin #stdout 0.06s 215488KB
stdin
Standard input is empty
stdout
0 0 0 0 0 0 0 0 0 0 
0 0 1 1 1 1 1 1 1 1 
0 1 1 2 2 2 2 2 2 2 
0 1 2 2 2 3 3 3 3 3 
0 1 2 3 3 3 3 4 4 4 
0 1 2 3 3 4 4 4 4 4