Photo Day
It's Photo Day!
As there are many classes to get through, the photographer has set up some rows of benches and has a target seating chart, with taller people in the back row and shorter people up front.
Students start queuing up, first to have their individual photo taken. After, they are directed to one of the benches for the group photo, where they will "slot in" to their row. Students will only move right to left when slotting into their row.
In the interest of time, it's best if the queue is arranged in such a way that people don't have to shimmy past each other, such as if there are 2 people already on a row, and then the next student in the queue is expected to squeeze in between them.
Given the ordering of the queue and the target seating chart, is it possible to achieve the target seating plan without having anyone have to squeeze past another student?
Input
The first line consists of 2 integers, and
. These are the number of rows, and how many students are in each row in the target seating chart.
.
The next lines consist of
names. Names will only contain uppercase and lowercase alphabetical characters and contain between 1 and 10 characters. All names are unique.
The next line consists of names, and lists the order of the queue of students. The first name in the line is the first student in the queue. All names in the target seating chart will be in the queue.
Output
Output YES or NO, depending on if we can achieve the target seating plan without having anyone have to squeeze past another student.
Example 1
Input
2 3
Bob Fred Alice
Luke Jane Tim
Bob Fred Luke Tim Alice Jane
Output
NO
This queue will cause Jane to have to squeeze between students. Here is how the seats will look like after each student goes into their row, with empty seats marked with underscores.
Bob is first up:
Bob ____ _____
____ ____ ___
Next is Fred:
Bob Fred _____
____ ____ ___
Next is Luke:
Bob Fred _____
Luke ____ ___
Then Tim:
Bob Fred _____
Luke ____ Tim
Now, it is not possible for Jane to get in position without having to squeeze past Tim.
Example 2
Input
2 3
Bob Fred Alice
Luke Jane Tim
Bob Fred Luke Alice Jane Tim
Output
YES
In this ordering, no one will have to squeeze past another student.
Example 3
Input
1 4
Bob Fred Alice Tim
Fred Alice Tim Bob
Output
NO
After the first 3 people make their way into the row, the seats will look like this:
___ Fred Alice Tim
For bob to slot in, since students only move right to left when slotting into their row, will actually have to squeeze past Tim, Alice, and Fred to make it to their spot.
Comments