Advertisement
jusohatam

Untitled

Sep 29th, 2020
824
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.53 KB | None | 0 0
  1. def solution(n, m, arr1, arr2):
  2.     d = {}
  3.  
  4.     def _lcs(i, j):
  5.         if d.get((i, j), None):
  6.             return d[(i, j)]
  7.         if i >= n or j >= m:
  8.             res = 0
  9.         elif arr1[i] == arr2[j]:
  10.             res = 1 + _lcs(i+1, j+1)
  11.         else:
  12.             res = max(_lcs(i, j+1), _lcs(i+1, j))
  13.         d[(i, j)] = res
  14.         return res
  15.  
  16.     return _lcs(0, 0)
  17.  
  18.  
  19. n = int(input())
  20. arr1 = list(map(int, input().split()))
  21. m = int(input())
  22. arr2 = list(map(int, input().split()))
  23. print(solution(n, m, arr1, arr2))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement