#C143. Simulate MySQL Full-Text Search

    ID: 43933 Type: Default 1000ms 256MiB

Simulate MySQL Full-Text Search

You are given a search query and a set of database records, where each record contains a title and content. Your task is to simulate a simplified version of MySQL's full-text search functionality. In this simulation, the search should be case-insensitive and should return only those records that contain all of the query words.

Details:

  • If the query string is empty, no records should be returned.
  • The search is case-insensitive. This means, for matching the words, you should convert both the query and the record fields to lowercase.
  • A record is considered a match only if every word in the query appears somewhere in the record (in either the title or the content).

Example:

For example, given a query Python powerful and records including a record with title "Learn Python" and content "Python is a powerful programming language.", the record is returned because both "Python" and "powerful" are found in the record.

Your solution should read input from standard input and output the result to standard output.

inputFormat

The input is given via stdin with the following format:

  1. The first line contains a string query.
  2. The second line contains an integer n representing the number of records.
  3. The next 2*n lines describe the records. For each record, the first line is the title and the second line is the content.

For example:

Python
4
Learn Python
Python is a powerful programming language.
Dive into MySQL
MySQL supports full-text search for efficient search capabilities.
Effective Java
Java is widely used in large scale applications.
PHP Development
Build dynamic websites using PHP and MySQL.

outputFormat

The output should be printed to stdout and have the following format:

  1. The first line contains an integer m which is the number of records that match the query.
  2. For each matching record, print two lines: the first line is the record's title and the second line is its content.

If no records match, output a single line with 0.

## sample
Python
4
Learn Python
Python is a powerful programming language.
Dive into MySQL
MySQL supports full-text search for efficient search capabilities.
Effective Java
Java is widely used in large scale applications.
PHP Development
Build dynamic websites using PHP and MySQL.
1

Learn Python Python is a powerful programming language.

</p>