Cash Dispenser
InterviewTime limit3sMemory limit128 MB
Count 4-digit PINs whose digits appear as a subsequence of every recorded finger-movement sequence.
- Level
Medium5 of 10
- Topics
- String, Dynamic programming, Brute force
- Solved
- No attempts yet
Problem
A bank's clients withdraw money from cash dispensers using a card and a 4-digit PIN. To protect clients from thieves who intercept the camera footage, a client makes extra, redundant finger movements over the keypad while entering the PIN. The camera cannot tell which keys were actually pressed; it records only the sequence of keys the finger passes over. As a result, the PIN usually cannot be determined uniquely.
For example, if a client moves his finger over key 1 and then over key 5, he could have entered any of these PINs: 1111, 1115, 1155, 1555, 5555. In other words, a PIN is consistent with a recorded movement when its four digits can be read off the recorded key sequence in order: each keypress happens over one of the keys the finger passes over, and the keypresses follow the direction of movement (a key may be pressed several times in a row while the finger rests on it).
You are given several recordings of the finger movements the same client made while entering the same PIN. Determine how many distinct 4-digit PINs are consistent with every recording, i.e. how many 4-digit codes could have produced all of the recorded movement sequences.
Input
The first line contains an integer , the number of recorded scenes of the client entering the PIN ().
Each of the next lines describes one scene as two values separated by a single space: an integer , the length of the movement sequence (), followed by a -digit number whose consecutive digits are the keys the finger passed over, in order.
The total length of all sequences does not exceed .
Output
Print a single integer: the number of 4-digit PIN codes that are consistent with all of the recorded movement sequences.