#K64717. Longest Common Book Sequence Analysis

    ID: 32038 Type: Default 1000ms 256MiB

Longest Common Book Sequence Analysis

Longest Common Book Sequence Analysis

You are given a log of users' book viewing records. Each record consists of a user ID and a book ID. For each user, the records are in chronological order. Your task is to find the length of the longest contiguous sequence of book views that appears at least twice across all user sequences (note that multiple appearances in a single user’s log are counted separately).

For example, consider the following log entries:

User 1: 5, 6, 7
User 2: 5, 6, 8
User 3: 7, 6

Here the subsequence [5, 6] appears in both User 1 and User 2, so the answer is 2. If no subsequence appears at least twice, output 0.

Note: A contiguous subsequence is defined as a sequence of consecutive books in a user's log. The same sequence may occur more than once in the same user’s log and across different users.

The answer should be computed using a brute force approach for small input sizes. Efficiency is not a primary concern in this problem.

inputFormat

The first line contains a single integer n, the number of log entries.

Each of the next n lines contains two integers separated by space: the user ID and the book ID, in the order they were viewed. The order of the entries for the same user represents the viewing order.

outputFormat

Output a single integer, the length of the longest contiguous sequence of book views that appears at least twice. If no such sequence exists, output 0.

## sample
8
1 5
1 6
1 7
2 5
2 6
2 8
3 7
3 6
2