P27701 [洛谷] [Beginner Contest #25] SQL Parser
普及+
题目描述

Background

SQL is a powerful database query language. In this problem, you need to implement a SQL parser that supports partial functionalities.

Problem Description

A database can be considered as a collection of several two-dimensional tables.

For example, a student information management system database might include the following two 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 is called the header, and each subsequent row represents a set of information in the table. For example, the second row of basic_info indicates that there is a student named Fusu with a student ID (sid) of 001 and a grade (grade) of 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 consider all attributes within the table to be given in string format.

You need to support query statements in the following format:

select [columns] from [table_name] where [header]=x

In the above statement, columns are several headers, table_name is the table name, header is a header, and x is the given condition. It means to query all rows in the table_name table where the header column is x, and output the information in all columns columns of these rows.

For example, if the following statement is executed on the tables in the example:

select name from basic_info where grade=1

It means to query all rows in the basic_info table where the grade column is 1, and output their name column. The result is:

Fusu

If the following statement is executed:

select name,sid,grade from basic_info where grade=2

Then it finds all rows in the basic_info table where the grade is 2, and outputs their name, sid, and grade columns in order. The result is:

Maxmilite 002 2
Expect2004 003 2

It can be seen that the first set of parameters columns in the select statement can be multiple header names. In this case, the corresponding column information should be output in the order given by columns. When multiple rows meet the requirements through the condition after where, the required information for each row should be output in the order of the table's input (i.e., from top to bottom).

You need to implement this SQL statement parser and provide the SQL query results. Note that we guarantee that there is only one table name in the query statement, and there is exactly one equality condition after where.

Input Format

The first line of input is an integer n, indicating the number of tables in the database.

Next, the information for each table is given sequentially:
- The first line is a string table_name, indicating the table's name.
- The second line contains two integers x and y, indicating the table size is x rows and y columns.
- The third line contains y strings, indicating the table's headers.
- The next x - 1 lines, each containing y strings, give the information for each row in the table.

Next is an integer m, indicating the number of SQL statements.

The next m lines, each containing one SQL statement in the format:

select [columns] from [table_name] where [header]=x

It is guaranteed that columns, table_name, header, and x do not contain spaces within these parameters.

The data guarantees that the query statement is legal, i.e., the headers, table names, and conditions provided by columns, table_name, and header exist, and the given query condition can find at least one row that meets the criteria, i.e., the query result is not empty.

It is guaranteed that there are no duplicate header names in columns.

Output Format

For each query, output several lines, each representing a query result.

In a query result line, if there are multiple columns of information to output, separate the strings with a space.

Sample Input and Output

Sample Input #1

1
basic_info
4 3
name sid grade
Fusu 001 1
Maxmilite 002 2
Expect2004 003 2
2
select name from basic_info where grade=1
select sid,name from basic_info where grade=2

Sample Output #1

Fusu
002 Maxmilite
003 Expect2004

Sample Input #2

2
basic_info
4 3
name sid grade
Fusu 001 1
Maxmilite 002 2
Expect2004 003 2
GPA
2 2
sid GPA
001 77.88
1
select GPA from GPA where sid=001

Sample Output #2

77.88

Sample Input #3

2
show_corner
3 2
h1 h2
1 2
1 2
show_corner2
3 1
h0
1
1
2
select h1,h2 from show_corner where h1=1
select h0 from show_corner2 where h0=1

Sample Output #3

1 2
1 2
1
1

Notes

Explanation of Sample 3

The input tables may have several duplicate rows, and you do not need to handle these rows specially. You can consider them as distinct. Each query should output all rows that meet the requirements. For example, in the first SQL statement, the row 1 2 appears twice and meets the criteria, so it is output twice as 1 2.

Data Size and Constraints

Test Case Number Number of Tables is 1 Number of Headers is 1 Only One Header in columns
1, 2 ✓ ✓ ✓
3, 4 ✓ × ✓
5, 6 × ✓ ✓
7, 8 × × ✓
9, 10 × × ×

For all test data, the following guarantees hold:

  • $1 \leq n \leq 10$.
  • $1 \leq x \leq 100$, $1 \leq y \leq 10$.
  • $1 \leq m \leq 1000$.
  • The length of the table name table_name does not exceed 100.
  • The length of the table content and headers does not exceed 10, and the length of columns does not exceed 100.
  • Except for columns, the input strings do not contain commas ,.
  • The input strings are all visible characters within the ASCII range of 32 to 126 (inclusive), and do not contain the symbol =.

Hints

Be mindful of the impact of large data output on program efficiency and choose an appropriate output method to avoid timeouts.

输入格式

见题面 Input 部分

输出格式

见题面 Output 部分

样例输入
1 basic_info 4 3 name sid grade Fusu 001 1 Maxmilite 002 2 Expect2004 003 2 2 select name from basic_info where grade=1 select sid,name from basic_info where grade=2
样例输出
Fusu 002 Maxmilite 003 Expect2004