-
Notifications
You must be signed in to change notification settings - Fork 6
/
CrossCountry.cpp
57 lines (53 loc) · 1.22 KB
/
CrossCountry.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
#include <iostream>
#include <climits>
#include <vector>
using namespace std;
#define SIZE 3000
long long lcs[SIZE][SIZE];
long long longestCommonSubsequence(vector<long long>& agnes, vector<long long>& curRoute) {
for(int i = 0; i <= agnes.size(); i++) {
for(int j = 0; j <= curRoute.size(); j++) {
if(i == 0 || j == 0)
lcs[i][j] = 0;
else if(agnes[i-1] == curRoute[j-1])
lcs[i][j] = 1 + lcs[i-1][j-1];
else
lcs[i][j] = std::max(lcs[i-1][j], lcs[i][j-1]);
}
}
return lcs[agnes.size()][curRoute.size()];
}
int main() {
long long T;
cin >> T;
while(T--) {
vector<long long> agnes;
vector<vector<long long> > others;
long long N;
while(true) {
cin >> N;
if(N == 0)
break;
agnes.push_back(N);
}
while(true) {
vector<long long> tmp;
cin >> N;
if(N == 0)
break;
tmp.push_back(N);
while(true) {
cin >> N;
if(N == 0)
break;
tmp.push_back(N);
}
others.push_back(tmp);
}
long long ans = 0;
for(long long i = 0; i < others.size(); i++)
ans = std::max(ans, longestCommonSubsequence(agnes, others[i]));
cout << ans << endl;
}
return 0;
}