Colorful Chain
InterviewTime limit3sMemory limit512 MB
Count the contiguous segments that hold exactly the required number of links of each listed color and no other colors.
- Level
Medium4 of 10
- Topics
- Sliding window, Hash map, Array
- Solved
- No attempts yet
Problem
Bytie loves playing with colorful chains. He already owns quite a collection, and he likes some of them more than others. A chain is a row of colored links.
Byteasar has noticed that Bytie's taste is very precise. Bytie calls a contiguous fragment of a chain nice if it contains exactly links of color , exactly links of color , , exactly links of color , and no links of any other color. The appeal of a chain is the number of nice contiguous fragments it contains.
After many attempts Byteasar worked out the values and . He now wants to buy a new chain, so he asks you for a program that helps him shop.
Input
The first line contains two integers and separated by a single space (): the length of the chain and the length of the description of a nice fragment.
The second line contains integers separated by single spaces ().
The third line contains integers separated by single spaces (, and whenever ).
The sequences and define a nice fragment: it has to contain exactly links of color .
The fourth line contains integers separated by single spaces (), the colors of the successive links of the chain.
Output
Print a single integer, the number of nice contiguous fragments in the chain.
Hint
In the first example the nice fragments are 2 1 3 1 and 1 3 1 2, so the answer is 2.