Hide

Problem I
Seattle Berries

Your aunt has a berry farm outside of Seattle where each row of berry bushes has been assigned a number and was planted at a different time. Your mom has sent you and your siblings to help your aunt on her berry farm this summer. Your aunt has predicted the order in which the rows of berry bushes should be picked based on when she planted them. However, you and your siblings disagree with her prediction once you see the bushes. You tell your aunt the way in which you would pick the berries. Your aunt decides to compare all of your picking orders to hers to determine the best way to pick the berries.

Your aunt decides to assign scores to each of your orderings by determining the longest (not necessarily continuous) sequence of bushes for which both your ordering and your aunt’s orderings are the same. For example, if your aunt has the picking order 3 4 2 1 and your sister has the picking order 1 4 2 3, then your sister would receive a score of 2 because both your aunt’s and your sister’s orderings contain the subsequence "4 2" in that order. If your brother, on the other hand, came up with the ordering 1 2 3 4, his ordering would also receive a score of 2 because both your aunt’s and his orderings contain the subsequence "3 4".

Unfortunately for your aunt, she has too many bushes and you have too many siblings to assign scores to all of these orderings by hand. Therefore, she’s asked you to create a program that will help her assign scores to all of the orders.

Input

Each input file contains one test case. The first line contains the single integer $n$ ($2 \leq n \leq 200$), the number of berry bushes. The rest of the lines contain $n$ space-separated integers and are a permutation of the integers $1, \dots , n$. The first set of $n$ numbers are the are your aunt’s orderings of bushes. each of the sets of numbers after that are the orderings created by you and each of your siblings.

Output

For each ordering provided by you or one of your siblings, output in its own line the integer corresponding to the score of that ordering.

Sample Input 1 Sample Output 1
5
4 3 1 5 2
1 4 5 2 3
5 2 1 3 4
4 2 3 5 1
2 5 3 4 1
2
3
4
3
Sample Input 2 Sample Output 2
10
1 2 3 4 5 6 7 8 9 10
8 4 5 7 1 6 10 3 9 2
6 1 9 7 3 4 2 8 5 10
1 2 3 7 8 9 10 5 4 6
9 1 4 5 2 7 8 10 6 3
6 7 4 1 10 5 8 2 9 3
4
5
7
6
4

Please log in to submit a solution to this problem

Log in