Madness
Time limit2sMemory limit1024 MB
Given apartment ids and lit windows on a grid, find the fewest apartments to change so a target lit/dark pattern appears as a sub-rectangle.
- Level
Medium7 of 10
- Topics
- Matrix, String matching, Hash map
- Solved
- No attempts yet
Problem
Seryozha wanted to do something crazy. For example, make a nice word appear on the wall of the building opposite.
He drew on graph paper a map of an grid showing which windows should be lit and which should be dark. Seryozha counts a word as written on the building if, inside some rectangle, the pattern of lit and dark windows matches his drawing. The state of the other windows does not matter.
To switch a window on or off, Seryozha must agree with the owner of the apartment. After that, he can switch the lights in any windows of that apartment in any way he likes.
He has a floor plan and knows which apartment each window belongs to. Each apartment consists of several adjacent windows on the same floor.
What is the minimum number of owners Seryozha must agree with?
Input
The first line contains integers and (). The next lines each contain integers. The -th number in line is the apartment number of the -th window from the left on floor of the building. Apartment numbers are positive integers not exceeding 5000.
The next lines each contain a string of characters. '#' means the light is on, and '.' means it is off.
The next line contains integers and (, ). The next lines each contain a string of characters. '#' means that window must be lit in the word, and '.' means it must be dark.
Output
Print one integer: the minimum number of apartments whose owners Seryozha must agree with.