#P3815. [FJOI2017] 回文子序列问题
[FJOI2017] 回文子序列问题
Description
Let X and Y be two given sequences of positive integers and .
A common subsequence of X and Y is a sequence , where and .
A sequence is called a palindrome if it reads the same forward and backward. For example, the sequence (1, 3, 5, 3, 1) is a palindrome. A common palindromic subsequence of X and Y is a common subsequence of X and Y that is also a palindrome. Among all common palindromic subsequences of X and Y, those with the maximum length are called a longest common palindromic subsequence (LCPS) of X and Y. In general, there may be multiple LCPSs. For example, for the given sequences and , the subsequences (1, 3, 1) and (1, 9, 1) are both LCPSs of X and Y.
Our task is: given sequences X and Y, determine the length of a longest common palindromic subsequence.
For the given two sequences and , compute the length of their longest common palindromic subsequence.
Input Format
The file contains multiple test cases (). For each test case, the first line contains a positive integer (), indicating that the lengths of the two input sequences X and Y are both . The next two lines give the input sequences and , respectively. Each element in the sequences is a positive integer (). The file ends with a 0.
Output Format
For each test case, output the computed length of the longest common palindromic subsequence, one length per line.
5
1 9 3 9 1
1 3 9 3 1
4
1 9 3 9
1 3 9 3
0
3
1
Hint
Translated by ChatGPT 5
京公网安备 11011102002149号