Skip to content

Instantly share code, notes, and snippets.

@KT-Yeh
Created March 20, 2014 08:33
Show Gist options
  • Save KT-Yeh/9659544 to your computer and use it in GitHub Desktop.
Save KT-Yeh/9659544 to your computer and use it in GitHub Desktop.
#include <cstdio>
#include <algorithm>
using namespace std;
int N, pos, Ans[30], num[30], LCS[30][30] = {0};
int main()
{
scanf("%d", &N);
for (int i = 1; i <= N; ++i) {
scanf("%d", &pos);
Ans[pos] = i;
}
while (scanf("%d", &pos) != EOF) {
num[pos] = 1;
for (int i = 2; i <= N; ++i) {
scanf("%d", &pos);
num[pos] = i;
}
int Max = 0;
for (int i = 1; i <= N; ++i) {
for (int j = 1; j <= N; ++j) {
if (Ans[i] == num[j])
LCS[i][j] = LCS[i-1][j-1] + 1;
else
LCS[i][j] = max(LCS[i-1][j], LCS[i][j-1]);
if (LCS[i][j] > Max) Max = LCS[i][j];
}
}
printf("%d\n", Max);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment