-
Notifications
You must be signed in to change notification settings - Fork 13
/
LCDS.cpp
97 lines (52 loc) · 1.53 KB
/
LCDS.cpp
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include<iostream>
#include<stdio.h>
using namespace std;
long min(long long x[],int n)
{
long long q=x[0];
for(int i=1;i<n;i++)
{
if(q>x[i])
q=x[i];
}
return q;
}
int LCS(long long x[],int n,long long y[],int m)
{
int c[n+1][m+1];
for(int i=1;i<=n;i++)
c[i][0]=0;
for(int j=0;j<=m;j++)
c[0][j]=0;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
{
if(x[i-1]==y[j-1])
c[i][j]=c[i-1][j-1]+1;
else if(c[i-1][j]>=c[i][j-1])
c[i][j]=c[i-1][j];
else
c[i][j]=c[i][j-1];
}
return c[n][m];
}
int main()
{
int n,m;
scanf("%d%d",&n,&m);
long long x[n],y[m];
for(int i=0;i<n;i++)
cin>>x[i];
for(int j=0;j<m;j++)
cin>>y[j];
long long k=min(x,n);
long long p=min(y,m);
for(int i=0;i<n;i++)
x[i]=x[i]-k;
for(int j=0;j<m;j++)
y[j]=y[j]-p;
int len=LCS(x,n,y,m);
printf("%d\n",len+1);
system ("pause");
return 0;
}