Longest Common Subsequence

View as PDF

Submit solution

Points: 7
Time limit: 1.0s
Memory limit: 256M

Author:
Problem type

Solve the Longest Common Subsequence problem.

Input Specification

Line 1: N, M (1 \le N, M \le 1\,000)

Line 2: N nonnegative integers \le 1\,000, the first sequence.

Line 3: M nonnegative integers \le 1\,000, the second sequence.

Output Specification

Line 1: The length of the LCS of the first and second sequences.

Sample Input

5 3
1 4 3 4 2
2 1 3

Sample Output

2

Comments


  • -1
    Redactedvoid  commented on Feb. 24, 2026, 4:50 p.m. edited

    Can anyone explain the input? I am confused.


    • -1
      do_ur_homwork  commented on Feb. 24, 2026, 8:49 p.m. edit 2

      The first line is N and M

      The seccond line is N numbers aka your first sequence/array

      The third line is M numbers aka your seccond sequence/array

      You are trying to find the Longest Common Subsequence


  • -18
    bobhob314  commented on June 19, 2015, 7:32 p.m. edited

    This comment is hidden due to too much negative feedback. Show it anyway.


    • 21
      kobortor  commented on June 19, 2015, 8:04 p.m. edited

      The test case can have multiple digit numbers you know

      print(input()) gives

      9 1 1 0 2 3 10 3 0

      not

      9 1 1 0 2 3 1 0 3 0