Problem 890
Linked List Components
Medium · 20 pts · Linked List, Hash Table
Problem
A linked list holds distinct values. You are also given a subset G of those values. A component is a maximal group of values from G that sit next to each other in the list. Count the components. Put G in a hash set and walk the list once: a component ends at a node whose value is in G when the next node is missing or its value is not in G, so count those nodes.
Input
The first line contains N, the list length. The second line contains the N distinct values from head to tail. The third line contains M. The fourth line contains the M values of G.
Output
Print the number of components.
Constraints
1 <= M <= N <= 10000; values are distinct integers from 0 to N - 1; G is a subset of the list values
Example input
4 0 1 2 3 3 0 1 3
Example output
2
Explanation
In the list 0 1 2 3 with G = 0 1 3, the values 0 and 1 are neighbours and form one component, and 3 forms another. So the answer is 2.