Advertisement
Malinovsky239

Untitled

Jan 15th, 2012
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.73 KB | None | 0 0
  1. #include <cstdio>
  2. #include <iostream>
  3. #include <algorithm>
  4. #include <cassert>
  5.  
  6. #define N 5005
  7.  
  8. using namespace std;
  9.  
  10. int n, m, a[N], b[N], dp[N][N], res;
  11.  
  12. int main() {
  13.     freopen("interview.in", "r", stdin);
  14.     freopen("interview.out", "w", stdout);
  15.  
  16.     cin >> n >> m;
  17.     for (int i = 0; i < n; i++)
  18.         cin >> a[i];
  19.  
  20.     for (int j = 0; j < m; j++)
  21.         cin >> b[j];
  22.  
  23.     for (int i = 0; i < n; i++)
  24.         for (int j = 0; j < m; j++) {          
  25.             if (a[i] == b[j]) {
  26.                 dp[i][j] = 1;
  27.                 for (int k = 0; k < i; k++)
  28.                     for (int t = 0; t < j; t++) {
  29.                         if (a[k] == b[t] && a[k] < a[i]) {
  30.                             dp[i][j] = max(dp[i][j], dp[k][t] + 1);
  31.                         }
  32.                     }
  33.             }  
  34.             res = max(dp[i][j], res);
  35.         }
  36.  
  37.     cout << res << endl;
  38.  
  39.     return 0;
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement