Genome
Time limit2sMemory limit512 MB
Find the length of the longest sequence that appears as a subsequence in every given permutation.
- Level
Medium6 of 10
- Topics
- Graph, Dynamic programming
- Solved
- No attempts yet
Problem
In comparative genomics, biologists want to find a gene sequence that is conserved across a group of species.
Let be a set of integers where each integer is one gene. There are species , and each species is given as a permutation of . That permutation is the order of the genes in .
A subsequence of an integer sequence is obtained by omitting none, one, or more integers from the original sequence. An integer sequence is a conserved gene sequence of the species if it is a subsequence of for every . Find the length of a longest conserved gene sequence of the species.
Input
The first line contains two integers and separated by a space (, ).
Each of the next lines contains a permutation of , with a space between two adjacent integers.
Output
Print one integer, the length of a longest conserved gene sequence.
Hint
Consider these 3 species.
- 5, 3, 4, 1, 2
- 2, 5, 4, 3, 1
- 5, 2, 3, 1, 4
The four sequences
- 5, 1
- 5, 3
- 5, 4
- 3, 1
are all conserved gene sequences of the 3 species, but none of them is longest. A longest conserved gene sequence of the 3 species is 5, 3, 1.