Advertisement
jusohatam

Untitled

Sep 29th, 2020
1,110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.50 KB | None | 0 0
  1. def solution(n, m, arr1, arr2):
  2.     dp = [[0 for _ in range(m+1)] for _ in range(n+1)]
  3.  
  4.     for i in range(n - 1, -1, -1):
  5.         for j in range(m - 1, -1, -1):
  6.             if arr1[i] == arr2[j]:
  7.                 dp[i][j] = dp[i + 1][j + 1] + 1
  8.     res = 0
  9.     for row in dp:
  10.         for elem in row:
  11.             res = max(res, elem)
  12.  
  13.     return res
  14.  
  15.  
  16. n = int(input())
  17. arr1 = list(map(int, input().split()))
  18. m = int(input())
  19. arr2 = list(map(int, input().split()))
  20. print(solution(n, m, arr1, arr2))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement