Book Search Program
Time limit3sMemory limit1024 MB
For each nested AND, OR, NOT search expression, count the books in the DB whose name, author, and tags satisfy every condition.
- Level
Medium4 of 10
- Topics
- Recursion, String, Implementation
- Solved
- No attempts yet
Problem
The PIMM game development club has a custom of graduates leaving their major textbooks in the club room. In the past, PIMM ran a book lending system so that new members could freely use used textbooks. However, last year, when Minkyu was club president, too many books piled up in the club room, and the lending system became hard to manage. Hearing this, Jonghyun started building a book search program that helps people find the books they need right away.
The book search program stores books and search requests as strings. The DB stores a string <book> that represents a book. <book> can be written as follows in Backus-Naur Form (BNF). In the table, the left side of "::=" is a symbol and the right side is an expression.
<book> ::= <name> " " <author> | <name> " " <author> " " <tags>
<tags> ::= <tag> | <tag> " " <tags>
<name> ::= <str>
<author> ::= <str>
<tag> ::= <str>
The symbols and expressions in this table have the following properties.
<str>is a string of at most 20 characters made of letters, digits, '-', and '_'.<name>is the name of the book.<author>is the author of the book.<tag>is a feature of the book.- No
<tag>is repeated within a single<tags>.
For example, if the string "1984 George_Orwell novel SF" is in the DB, it means a book named 1984 by George_Orwell has two features, novel and SF.
No two books in the DB can have the same <name> and <author> together. Note that several books can share the same <name> or the same <author>.
A search request <search> is also given as a string.
<method> ::= <AND> | <OR> | <NOT> | <nameCorrect> | <nameInclude> | <authorCorrect> | <tagCorrect>
<methods> ::= <method> | <method> ", " <methods>
<AND> ::= "A(" <methods> ")"
<OR> ::= "O(" <methods> ")"
<NOT> ::= "N(" <method> ")"
<nameCorrect> ::= "n:" <str>
<nameInclude> ::= "ni:" <str>
<authorCorrect> ::= "a:" <str>
<tagCorrect> ::= "t:" <str>
<search> ::= <methods>
The symbols and expressions in this table have the following properties.
<str>is a string of at most 20 characters made of letters, digits, '-', and '_'.<nameCorrect>returns true if the expression<str>equals the<name>of the given<book>, and false otherwise.<nameInclude>returns true if the expression<str>is contained in the<name>of the given<book>, and false otherwise.<authorCorrect>returns true if the expression<str>equals the<author>of the given<book>, and false otherwise.<tagCorrect>returns true if at least one<tag>in the<tags>of the given<book>equals the expression<str>, and false otherwise.<AND>returns true if every<method>in<methods>returns true, and false otherwise.<OR>returns true if at least one<method>in<methods>returns true, and false otherwise.<NOT>returns false if the<method>returns true, and true otherwise.<search>returns the number of<book>s in the DB for which every<method>in<methods>returns true.
Input
The first line contains , the number of <book> strings in the DB. The next lines each contain one <book>.
The next line contains , the number of search strings <search>. The next lines each contain one <search>.
Invalid strings are never given as input.
Output
For each <search>, print the result on its own line.
Constraints
Each <book> in the input is at most characters long.
Each <search> in the input is at most characters long.
All numbers in the input are integers.