Fine Dining Restaurant
Time limit3sMemory limit128 MB
For each banned serial number, count the digit comparisons the described naive left-to-right substring search performs against the concatenated string A.
- Level
Hard8 of 10
- Topics
- String matching, Trie, Prefix sum
- Solved
- No attempts yet
Problem
Sanggeun runs a fine dining restaurant. After a nuclear power plant accident people became very afraid of radiation, so the government decided that the radiation in kinds of ingredients is at a dangerous level and passed a law that keeps those ingredients out of cooking entirely.
Every ingredient carries a serial number made only of the digits 0 to 9. Sanggeun's restaurant makes a single dish, and he knows the serial number of every ingredient that goes into it.
Sanggeun has to check each ingredient for radioactive contamination. He is very lazy, so he concatenates the serial numbers of the ingredients in order into one string of length , and then only checks whether a banned serial number occurs inside .
Seonyeong, who runs a restaurant nearby, does this check automatically with a robot. Sanggeun asked her and borrowed it.
The robot checks whether the serial number occurs inside the serial number in the following order. The length of is .
- Take digit 1 through digit of and compare them with one digit at a time, front to back. The comparison stops when two digits differ or when the last digit has been compared. If the two strings are equal, the robot shows a success message and ends the check.
- If the check has not ended, compare digit 2 through digit the same way. If they are not equal either, go on with digit 3 through digit , then digit 4 through digit , in that order.
- The substring that gets cut out is sometimes shorter than . (The length of is 8 and the check starts at position 5.) Then
#is appended until the length is , and the comparison runs on that. For example, position 4 through position 10 of563232is232####. A#is equal to none of the digits 0 to 9. - If all starting positions are checked and no equal part is found, the robot shows a failure message and ends the check.
Every time the robot compares two digits, Sanggeun has to pay Seonyeong 1 won.
Find the amount Sanggeun has to pay while the robot checks the banned serial numbers one by one.
Input
The first line contains the length of the string obtained by concatenating the serial numbers of the ingredients, and the second line contains that string. ()
The third line contains the number of banned ingredients . () Each of the next lines contains one banned serial number.
Every serial number consists only of the digits 0 to 9. A single banned serial number is at most digits long, and the banned serial numbers are at most digits long in total.
Output
For each banned serial number, print the amount the robot's check costs, one per line, in the order given in the input.