← 返回 instacart 的题目列表Fix Backend Filtering for Book Search Results
类型:online_judge
Problem: Fix Backend Filtering for Book Search Results
You are fixing a bug in a book management system. The frontend calls a backend search API and renders the returned books, but the backend currently does not correctly filter the results before returning them.
Implement the backend filtering logic. Given a list of books and a search request, return the IDs of all books that match the request.
Book fields
Each book has:
id: integer, unique book ID
title: string
author: string
genre: string
available: 0 or 1, whether the book is available
Search request
The request contains:
query: search keyword
genre: genre filter; * means no genre filter
available: availability filter:
-1: no availability filter
0: only unavailable books
1: only available books
Filtering rules
A book should be returned only if all conditions hold:
If query is non-empty, either title or author contains query, case-insensitively.
If genre != "*", the book's genre must exactly equal the requested genre, case-sensitive.
If available != -1, the book's available must equal the requested value.
Return IDs in the same order as the input books.
Input format
n
id|title|author|genre|available
...
query
genre
available
Constraints:
1 <= n <= 10^5
1 <= len(title), len(author), len(genre) <= 100
0 <= len(query) <= 100
IDs are unique
Output format
Print matching book IDs separated by spaces.
If no book matches, print an empty line.
Example
Input:
5
1|Clean Code|Robert Martin|Programming|1
2|The Pragmatic Programmer|Andrew Hunt|Programming|0
3|Harry Potter|J. K. Rowling|Fantasy|1
4|Designing Data-Intensive Applications|Martin Kleppmann|Programming|1
5|The Hobbit|J. R. R. Tolkien|Fantasy|0
martin
Programming
1
Output:
1 4
Example
Input
5
1|Clean Code|Robert Martin|Programming|1
2|The Pragmatic Programmer|Andrew Hunt|Programming|0
3|Harry Potter|J. K. Rowling|Fantasy|1
4|Designing Data-Intensive Applications|Martin Kleppmann|Programming|1
5|The Hobbit|J. R. R. Tolkien|Fantasy|0
martin
Programming
1
Output
1 4