Common Subsequence
InterviewTime limit1sMemory limit128 MB
Compute the length of the longest common subsequence of two given strings for multiple test cases.
- Level
Medium4 of 10
- Topics
- Dynamic programming, String
- Solved
- No attempts yet
Problem
A subsequence of a given sequence is that sequence with zero or more of its elements left out. Formally, given a sequence , a sequence is a subsequence of if there is a strictly increasing sequence of indices such that for every . For example, is a subsequence of via the index sequence .
Given two sequences and , find the length of a longest common subsequence of and (a sequence that is a subsequence of both).
Input
The input contains several data sets and continues until end of file. Each data set consists of two strings, each representing one sequence. The two strings of a data set, and consecutive data sets, are separated by any amount of white space (spaces, tabs, or newlines). Each string has length at most . The input is guaranteed to be well-formed.
Output
For each data set, print on its own line the length of a longest common subsequence of the two sequences.