Last active
August 25, 2017 20:09
-
-
Save mhmoodlan/54fc931d95332f299ceaa8c8e8dc05d4 to your computer and use it in GitHub Desktop.
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1007
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <bits/stdc++.h> | |
#define ll long long | |
#define sz(v) ((int) ((v).size())) | |
#define clr(v, d) memset(v, d, sizeof(v)) | |
#define lp(i, n) for(int i = 0; i < (int)(n); ++i) | |
#define rep(i, v) for(int i = 0; i < sz(v); ++i) | |
using namespace std; | |
const int MAX = 105; | |
const int OO = 1e9; | |
int cache[MAX][MAX]; | |
int a[MAX], b[MAX]; | |
int n1, n2; | |
int maxTower(int i, int j) { | |
if(i == n1 || j == n2) | |
return 0; | |
int &ret = cache[i][j]; | |
if(ret != -1) | |
return ret; | |
ret = -1*OO; | |
int ch0 = -1*OO; | |
if(a[i] == b[j]) | |
ch0 = 1 + maxTower(i+1, j+1); | |
int ch1 = maxTower(i+1, j); | |
int ch2 = maxTower(i, j+1); | |
return ret = max(max(ch0, ch1), ch2); | |
} | |
int main() { | |
cin>>n1>>n2; | |
int k = 1; | |
while(n1 != 0 && n2 !=0) { | |
clr(cache, -1); | |
lp(i, n1) | |
cin>>a[i]; | |
lp(i, n2) | |
cin>>b[i]; | |
cout << "Twin Towers #" << k << endl; | |
cout << "Number of Tiles : " << maxTower(0, 0) << endl << endl; | |
k++; | |
cin>>n1>>n2; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment