Logo Matching
Time limit2sMemory limit128 MB
Given a permutation pattern of length n and a sequence of m distinct heights, find all starting positions where a length-n window matches the relative order pattern.
- Level
Hard8 of 10
- Topics
- String matching, Array, Sorting
- Solved
- No attempts yet
Problem
As part of a new advertising campaign, a large company wants to place its logo somewhere in the city. The company will spend its entire yearly advertising budget on the logo, so it has to be enormous — one manager decided to use whole buildings as parts of it.
The logo consists of vertical stripes of pairwise different heights, numbered to from left to right. It is described by a permutation of : stripe is the shortest, stripe is the second shortest, and so on, up to stripe , the tallest. Only the relative order of the stripe heights matters, not their actual values.
There are buildings along the main street, and all of their heights are pairwise different. A contiguous block of consecutive buildings matches the logo when, inside that block, the building at position is the shortest, the building at position is the second shortest, and so on. For example, heights match the logo : the building at position (height ) is the shortest, the one at position is the second shortest, and the one at position is the tallest. Find every place where the logo matches the buildings.
Input
The first line contains two integers and ().
The second line contains integers , a permutation of (so and for ).
The third line contains integers , the heights of the buildings (); all are different.
Within each line the integers are separated by single spaces.
Output
On the first line print the number of matches .
On the second line print, in increasing order and separated by single spaces, the -based index of the building aligned with stripe number of the logo for each match — equivalently, the starting index of each matching block.
If , the second line must be empty.
Note

Both blocks and match the logo described by the permutation . In the first block the building at position (height ) is the shortest, the building at position (height ) is the second shortest, the building at position (height ) is the third shortest, and so on.