The Best Gift Is an Array!
InterviewTime limit2sMemory limit1024 MB
Find a contiguous subarray of a with the same length as p whose elements match p as a multiset, and print its start position or NO.
- Level
Medium4 of 10
- Topics
- Sliding window, Hash map, Array
- Solved
- No attempts yet
Statement
Dim Dimych's birthday is tomorrow, and Simka wants to give him an array of natural numbers. She hid the array inside his computer's system unit so he would not find it early. Nolik ran in and shuffled all the numbers, so the array is no longer a suitable gift. Nolik remembers the original array and will restore it if Simka solves a problem for him.
He gives her a multiset of numbers and asks her to find a contiguous subsegment of the same length in the gift array such that and are almost equal. Two multisets are almost equal if their elements can be rearranged so that the two match exactly. For example, and are almost equal, but and are not.
Input
The first line contains a single integer (), the number of elements in .
The second line contains integers (), the elements of .
The third line contains an integer (), the length of the gift array.
The fourth line contains integers (), the elements of the array.
Output
If the gift array contains a subsegment that satisfies the conditions, print YES on the first line. Otherwise, print NO. If the answer is YES, print the starting position of the subsegment on the second line. If there are several answers, you may print any of them.