P10486 [Beginner Contest #25] sql

Background

SQL is a powerful database query language. In this problem, you need to implement a SQL parser with partial functionality.

Description

A database can be seen as a collection of several 2D tables. For example, a database of a student information management system may have the following two 2D tables: `basic_info`: | name | sid | grade | | :-: | :-: | :-: | | Fusu | 001 | 1 | | Maxmilite | 002 | 2 | | Expect2004 | 003 | 2 | `GPA`: | sid | GPA | | :-: | :-: | | 001 | 77.88 | | 002 | 99.9 | | 003 | 99.7 | The first row of the table above is called the **header**. Each following row represents one record of the table. For example, the second row of `basic_info` means that there is a student whose name (`name`) is $\texttt{Fusu}$, student ID (`sid`) is $\texttt{001}$, and grade (`grade`) is $\texttt{1}$. A formal SQL language should support cross-table queries, but in this problem, you only need to support queries within a single table. We assume that all attributes in the table are given as strings. You need to support query statements in the following format: ```sql select [columns] from [table_name] where [header]=x ``` In the statement above, `columns` is a list of **headers**, `table_name` is the **table name**, `header` is a **header**, and $x$ is the given condition. It means: in the table `table_name`, find all rows whose value in the `header` column equals $x$, and output the information in the specified `columns` for those rows. For example, executing the following statement on the tables in the example above: ```sql select name from basic_info where grade=1 ``` means: in the `basic_info` table, find all rows where the `grade` column is $1$, and output their `name` column. Therefore the result is: ```plain Fusu ``` If you execute: ```sql select name,sid,grade from basic_info where grade=2 ``` then you find all rows in the `basic_info` table where `grade` is `2`, and output their `name`, `sid`, and `grade` in order. The result is: ```plain Maxmilite 002 2 Expect2004 003 2 ``` As you can see, the first parameter group `columns` in a `select` statement may contain multiple header names. In that case, you should output each corresponding column in the order given by `columns`. If the condition after `where` matches multiple rows, you should output the required information for each row in the input order of the table (from top to bottom). You need to implement this SQL statement parser and output the query results. Note that we guarantee that there is only one table name in each query statement, and there is exactly one equality condition after `where`.

Input Format

The first line contains an integer $n$, indicating the number of tables in the database. Then the information of each table is given in order: The first line is a string `table_name`, indicating the name of the table. The second line contains two integers $x, y$, indicating that the table has $x$ rows and $y$ columns. The third line contains $y$ strings, indicating the headers of the table. The next $x - 1$ lines each contain $y$ strings, giving the information of each row in the table. Next is an integer $m$, indicating the number of SQL statements. The next $m$ lines each contain one SQL statement, and the format is guaranteed to be: ```sql select [columns] from [table_name] where [header]=x ``` It is guaranteed that there are no spaces inside the four parameters `columns`, `table_name`, `header`, and `x`. The testdata guarantees that the query statements are valid. That is, the headers and table names provided by `columns`, `table_name`, and `header` all exist, and the given condition can always match at least one row, so the query result is non-empty. It is guaranteed that there are no duplicate header names in `columns`.

Output Format

For each query, output several lines in order, representing the query result. In one line of query result, if there are multiple columns to output, separate the strings with one space.

Explanation/Hint

### Explanation for Sample 3 The input tables may contain several duplicate rows. You do not need to handle these rows specially; you can treat them as different rows. Each query must output all rows that meet the condition. For example, in the first SQL statement, the row `1 2` appears twice and satisfies the condition, so `1 2` is output twice. ### Constraints and Conventions | Test Point ID | Number of tables is $1$ | Number of headers is $1$ | Only one header in `columns` | | :-: | :-: | :-: | :-: | | 1, 2 | $\checkmark$ | $\checkmark$ | $\checkmark$ | | 3, 4 | $\checkmark$ | $\times$ | $\checkmark$ | | 5, 6 | $\times$ | $\checkmark$ | $\checkmark$ | | 7, 8 | $\times$ | $\times$ | $\checkmark$ | | 9, 10 | $\times$ | $\times$ | $\times$ | For all testdata, it is guaranteed that: - $1 \leq n \leq 10$. - $1 \leq x \leq 100$, $1 \leq y \leq 10$. - $1 \leq m \leq 1000$. - The length of `table_name` does not exceed $100$. - The string lengths of the table contents and headers do not exceed $10$, and the length of `columns` does not exceed $100$. - Except for the `columns` information, the input strings do not contain commas `,`. - All input strings are visible characters, with ASCII range $33 \sim 126$ (inclusive), and do not contain the symbol `=`. ### Hint Please note the impact of large output on program efficiency. Choose an appropriate output method to avoid timeout. Translated by ChatGPT 5