#K9901. Minimum Herbs Discard

    ID: 39132 Type: Default 1000ms 256MiB

Minimum Herbs Discard

Minimum Herbs Discard

You are given two sequences of integers. The first sequence represents the gathered herbs, and the second sequence represents the required recipe. Your task is to determine the minimum number of herbs that must be discarded from the gathered herbs such that the remaining herbs (in their original order) contain the recipe as a subsequence.

Formally, let \(G = [g_1, g_2, \dots, g_n]\) be the sequence of gathered herbs and \(R = [r_1, r_2, \dots, r_m]\) be the recipe. You need to find the minimum number of herbs to discard, i.e., compute:

[ n - k ]

where \(k\) is the largest integer such that \(R\) is a subsequence of \(G\) (i.e., the matching herbs in order). Note: if either sequence is empty, the answer is defined accordingly.

Example: If gathered herbs are [1, 2, 3, 4, 5, 6, 7] and the recipe is [2, 4, 6, 7], then the maximum matching subsequence is [2, 4, 6, 7] and the number of discarded herbs is 7 - 4 = 3.

inputFormat

The input is given from standard input (stdin) in the following format:

  1. An integer \(n\) representing the number of gathered herbs.
  2. A line with \(n\) space-separated integers representing the gathered herbs.
  3. An integer \(m\) representing the number of herbs in the recipe.
  4. A line with \(m\) space-separated integers representing the recipe herbs.

Note that if \(n = 0\) or \(m = 0\), the corresponding line of integers may be empty.

outputFormat

Output a single integer to standard output (stdout): the minimum number of herbs that need to be discarded.

## sample
7
1 2 3 4 5 6 7
4
2 4 6 7
3