-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathA133(LCS)
62 lines (61 loc) · 1.1 KB
/
A133(LCS)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include<iostream>
#include<math.h>
#include<vector>
using namespace std;
int n1,n2,a;
int dp[101][101];
vector<int> tower1;
vector<int> tower2;
int dynamic(int a1,int a2)
{
if(a1<0||a2<0)
{
return 0;
}
if(dp[a1][a2]!=-1)
{
return dp[a1][a2];
}
if(tower1[a1]==tower2[a2])
{
dp[a1][a2]=dynamic(a1-1,a2-1)+1;
}
else
{
dp[a1][a2]=max(dynamic(a1-1,a2),dynamic(a1,a2-1));
}
return dp[a1][a2];
}
int main()
{
int k=0;
while(cin>>n1>>n2)
{
k++;
tower1.clear();
tower2.clear();
if(n1==0&&n2==0)
{
break;
}
for(int i=1; i<=n1; i++)
{
cin>>a;
tower1.push_back(a);
}
for(int i=1; i<=n2; i++)
{
cin>>a;
tower2.push_back(a);
}
for(int i=0; i<=n1; i++)
{
for(int o=0; o<=n2; o++)
{
dp[i][o]=-1;
}
}
cout<<"Twin Towers #"<<k<<endl;
cout<<"Number of Tiles : "<<dynamic(n1-1,n2-1)<<"\n\n";
}
}