Solidarity of the Happy Cats
시간 제한1초메모리 제한2048 MB
전선을 원래 순서대로 최소 개수의 칸에 배치하되, 영향을 주는 신호 종류를 가진 전선의 범위 안에 다른 전선이 들어가지 않도록 해야 한다.
문제
Professor Pattengale has run into a huge problem - his robot is getting wrong digital signals, causing it to move around unpredictably! His circuitry students, the Happy Cats, conclude that the wires in the motherboard are too close together. As an electrical current passes through a wire, it creates a magnetic field that induces currents in other nearby wires, causing false signals.
The Engineering Family finds that the motherboard has slots lined up in a row, and each slot has exactly one wire installed in it. Each wire has range , covering slots both to the left and to the right of that wire; other wires contained in this range may be affected by its magnetic field. Each wire also has a type of signal running through it.
There are also different types of signals being passed throughout the wires. Certain types of signals can impact other types of signals. For two different types and , if a signal of type can impact a signal of type , then a wire with signal type cannot be within the range of any wire of signal type , otherwise there will be interference. If type does not impact a signal of type , then wires with signal type are permitted to be within the range of any wire with signal type . Interference only happens when a signal of type impacts a signal with type , and the wire with signal type is within the range of the wire with signal type .
It is possible for a signal of type to impact a signal of type , but for a signal of type to not impact a signal of type . It is also possible for a signal of type to impact other wires of type .
The Engineering Family wants to create a new motherboard for the wires. The new motherboard will also comprise a single row of slots. All wires must be installed in the same order from left to right in the new motherboard as they were in the old motherboard. It is only possible to install at most one wire in any given slot. Some slots may be left empty. There must be no interference.
Given the original layout of the wires, compute the minimum number of slots in the new motherboard such that there is no interference.
입력
The first line of input contains two positive integers, and ().
lines follow, each with a pair of integers and (), the range and signal type of the -th wire. The wires are presented in order from left to right as they are installed in the old motherboard.
lines follow, each containing a binary string of length . The th line describes which signals impact signals of type . If the th digit is a , then signals of type do not impact signals of type . Otherwise, signals of type do impact signals of type .
출력
Output a single integer: the minimum number of slots the new motherboard needs to have to avoid interference.