fork download
  1. import java.util.*;
  2. import java.io.*;
  3.  
  4.  
  5. public class Main
  6. {
  7.  
  8. public static void main (String[] args) throws IOException
  9. {
  10. String str1 = br.readLine();
  11. String str2 = br.readLine();
  12.  
  13. long[][] dp = new long[str2.length()][str1.length()];
  14.  
  15. for(int i = 0 ; i < str1.length() ; i++)
  16. {
  17. if(i > 0)
  18. {
  19. if(str2.contains(Character.toString(str1.charAt(i))))
  20. dp[0][i] = dp[0][i-1] + 1;
  21. else
  22. dp[0][i] = dp[0][i-1];
  23. }
  24.  
  25. for(int j = 1 ; j < str2.length() ; j++)
  26. {
  27. if(str1.contains(Character.toString(str2.charAt(j))))
  28. dp[j][i] = dp[j-1][i] + 1;
  29. else
  30. dp[j][i] = dp[j-1][i];
  31. }
  32. }
  33.  
  34. for(int i = 0 ; i < str2.length() ; i++)
  35. {
  36. for(int j = 0 ; j < str1.length() ; j++)
  37. {
  38. System.out.print(dp[i][j]+" ");
  39. }
  40. System.out.println();
  41. }
  42.  
  43. System.out.println(dp[str2.length()-1][str1.length()-1]);
  44.  
  45. }
  46.  
  47. }
Success #stdin #stdout 0.12s 40068KB
stdin
ACAYKP
CAPCAK
stdout
0 1 2 2 3 4 
1 2 3 3 4 5 
2 3 4 4 5 6 
3 4 5 5 6 7 
4 5 6 6 7 8 
5 6 7 7 8 9 
9