#t382. 最长公共子序列(LCS)
最长公共子序列(LCS)
Longest Common Subsequence
Background
Special for beginners, ^_^
Problem Description
A subsequence of a given sequence is a sequence obtained by deleting some elements from the given sequence.
More precisely, given a sequence , another sequence is a subsequence of if there exists a strictly increasing sequence of indices such that for all :
For example, the sequence is a subsequence of , with the corresponding increasing index sequence .
Given two sequences and , when another sequence is a subsequence of both and , we call a common subsequence of and .
For example, if and , then the sequence is a common subsequence of and , and the sequence is also a common subsequence of and . Moreover, the latter is a longest common subsequence of and , because and have no common subsequence longer than 4.
Given two sequences and , find a longest common subsequence of and .
Input Format
The input consists of two lines. Each line is a string of uppercase letters of length at most 200, representing the sequences and .
Output Format
The first line outputs a non-negative integer: the length of the longest common subsequence found.
Sample Input 1
ABCBDAB
BDCABA
Sample Output 1
4
Constraints
1s, 1024KiB for each test case.
