#P4303. [AHOI2006] 基因匹配

    ID: 3238 远端评测题 1000ms 125MiB 尝试: 0 已通过: 0 难度: 7 上传者: 标签>2006各省省选树状数组安徽背包

[AHOI2006] 基因匹配

Description

Kaka dreamed last night that he and Keke arrived on another planet. On this planet, organisms have DNA sequences formed by countless types of bases (on Earth there are only 44 types). Even stranger, each type of base appears exactly 55 times in any given DNA sequence. Thus, if a DNA sequence consists of NN distinct types of bases, its length must be 5N5N.

After waking up, Kaka told Keke about this strange dream. Keke has been studying the gene matching problem in bioinformatics, so he decided to write a simple DNA matching program for organisms on this strange planet.

To describe the principle of gene matching, we first define the concept of a subsequence: if we arbitrarily pick some bases (characters) from a DNA sequence (string) ss and arrange them in the same order as in ss to form a new string uu, then uu is called a subsequence of ss. For two DNA sequences s1s_1 and s2s_2, if there exists a sequence uu that is a subsequence of both s1s_1 and s2s_2, then uu is called a common subsequence of s1s_1 and s2s_2.

Given two DNA sequences s1s_1 and s2s_2, the maximum match is defined as the length of the longest common subsequence of s1s_1 and s2s_2.

[Task] Write a program to:

  • Read two DNA sequences of equal length from the input file.
  • Compute their maximum match.
  • Print your result to the output file.

Input Format

The first line contains an integer NN, meaning that a certain organism on this planet uses NN distinct types of bases, numbered by the integers 1N1 \ldots N.

The next two lines each describe a DNA sequence: each contains 5N5N integers in the range 1N1 \ldots N, and every integer appears exactly 55 times in the corresponding sequence.

Output Format

Output a single integer: the maximum match of the two DNA sequences.

2
1 1 1 1 1 2 2 2 2 2 
1 1 1 2 2 2 2 2 1 1 

8

Hint

1N200001 \leq N \leq 20000.

Translated by ChatGPT 5