Advertisement
Guest User

Untitled

a guest
Mar 5th, 2015
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.26 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.lang.String;
  3. import java.lang.Math;
  4.  
  5. /*
  6. class TreeNode
  7. {
  8. int val;
  9. TreeNode left;
  10. TreeNode right;
  11. TreeNode(int x) { val = x; left = null; right = null;}
  12. }
  13. class ListNode
  14. {
  15. int val;
  16. ListNode next;
  17. ListNode(int x){val = x; next = null;}
  18. }
  19. */
  20. public class Solution {
  21.  
  22.  
  23. public static void main(String[] args)
  24. {
  25. //int T;
  26. Scanner jin = new Scanner(System.in);
  27. //T = jin.nextInt();
  28. String str1 = jin.next();
  29. String str2 = jin.next();
  30. int len = str1.length();
  31. int[][] array = new int[len+1][len+1];
  32. for(int i = 0; i < len+1; i++)
  33. array[0][i] = array[i][0] = 0;
  34. for(int i = 0; i < len; i++)
  35. {
  36. for(int j = 0; j < len; j++)
  37. {
  38. if (str1.charAt(i) == str2.charAt(j)) {
  39. array[i+1][j+1] = array[i][j] + 1;
  40. }
  41. else {
  42. array[i+1][j+1] = Math.max(array[i][j+1], array[i+1][j]);
  43. }
  44. }
  45. }
  46. System.out.println(array[len][len]);
  47. array = null;
  48. }
  49.  
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement