问题 B: Database

内存限制:1024 MB 时间限制:2 S
题面:Markdown 评测方式:文本比较 上传者:
提交:1 通过:1

题目描述

YangQiShaoNian is a fan of data structures. After studying the course $\sf CMU 15-445$ $\sf Introduction$ $\sf to$ $\sf Database$ $\sf Systems$, he decided to lead you in front of the screen to create a simple SQL statement executor. This database can be abstracted as a table with a variable number of rows and exactly $n$ columns. Each column of the table has a unique field name, and the field names are all different. You will need to handle various types of SQL statements. The statement formats are as follows: **1\. SQL Statements** **1.1.** $\tt insert$ **Statement** **Syntax** $\tt insert($field value$1,$field value$2,\dots ,$field value$n\tt)$ **Explanation** Inserts a new record into the table, which has exactly $n$ parameters. The $i$\-th parameter represents the value of the $i$\-th field, and each parameter is a string. The string contains only lowercase and uppercase letters, numbers, and underscores. **Example** If the table is currently: ![](https://uploadfiles.nowcoder.com/images/20240803/0_1722624979054/90366B69F7AB1FF4C0EAB5B70F3F1197) After executing $\tt insert(c,Yangqishaonian,Nowcoder)$, the table becomes: ![](https://uploadfiles.nowcoder.com/images/20240803/0_1722625008384/99A6C3DF67B62A4397DFB07E00DBE862) **1.2.** $\tt select$ **Statement** **Syntax** $\tt select($output field name$\tt ,$condition field name$\tt ,$condition value$)$ **Explanation** The output field name and condition field name are among the given $n$ field names. The condition value is a string. The string contains only lowercase and uppercase letters, numbers, and underscores. For a certain data, if its condition field is equal to the condition value, then add the corresponding data item of this data to the returned set. **Return Value** The result of the query is a sorted set of strings, sorted in the order of insertion. **Example** If the table is currently: ![](https://uploadfiles.nowcoder.com/images/20240803/0_1722625028416/76AF666831FD6F13DBD3E826D948411C) At this time, the return value of $\tt select(name, birth\_place,Beijing)$ is a set of $2$ strings, where the first string is $\tt Alice$ and the second string is $\tt Bob$. **1.3.** $\tt delete$ **Statement** **Syntax** $\tt delete($condition field name$\tt ,$ condition value$\tt )$ **Explanation** The condition field name is among the given $n$ field names. The condition value is a string. The string contains only lowercase and uppercase letters, numbers, and underscores. For a certain data, if its condition field is equal to the condition value, then it is deleted. **Example** If the table is currently: ![](https://uploadfiles.nowcoder.com/images/20240803/0_1722625049337/AD320F0EA152EE5E2C0EBA60F25B54D2) After executing $\tt delete(birth\_place,Beijing)$, the table becomes: ![](https://uploadfiles.nowcoder.com/images/20240803/0_1722625062622/9EEF4F3203A473D145E6EE9DD26E5255) **1.4.** $\tt select\_in$ **Statement** **Syntax** $\tt select\_in($output field name$\tt ,$condition field name$\tt ,$condition value set$\tt )$ **Explanation** The output field name and condition field name are among the given $n$ field names. The condition value set is a set of strings. For a certain data, if its condition field appears in the condition value set, then add the corresponding data item of this data to the returned set. **Return Value** The result of the query is a sorted set of strings, sorted in the order of insertion. **Note** For the third parameter, it can only nest $\tt select$ or $\tt select\_in$ statements. **Example** ![](https://uploadfiles.nowcoder.com/images/20240803/0_1722625076366/21361DDEB783C0CC5F7CB9463C83F61F) The return value of $\tt select\_in(D,A,select(B,C,2))$ is a set of $2$ strings, where the first string is $\tt x$ and the second string is $\tt z$. **1.5.** $\tt delete\_in$ **Statement** **Syntax** $\tt delete\_in($condition field name$\tt ,$condition value set$\tt )$ **Explanation** The condition field name is among the given $n$ field names. The condition value set is a set of strings. For a certain data, if its condition field appears in the condition value set, then it is deleted. **Note** For the second parameter, it can only nest $\tt select$ or $\tt select\_in$ statements. **Example** ![](https://uploadfiles.nowcoder.com/images/20240803/0_1722625090663/E8F044B435A9712ACD518B21B7E22487) After executing $\tt delete\_in(A,select(B,C,2))$, the table becomes: ![](https://uploadfiles.nowcoder.com/images/20240803/0_1722625102498/A752CD8651207E3ECA606B2E7FBE4FEF) **2\. Transaction Statements** In the database, each SQL statement must **belong to exactly one transaction**. The format of each transaction is as follows: 1.  $\tt begin()$ Statement --- Indicates the start of a transaction 2. Several SQL statements... --- All these SQL statements belong to the current transaction 3. $\tt commit()$ Statement or $\tt abort()$ Statement --- Indicates the end of a transaction The next three subsections will introduce the three types of statements mentioned above. **2.1.** $\tt begin$ **Statement** **Syntax** $\tt begin()$ **Explanation** Indicates the start of a new database transaction, and the first statement must be $\tt begin()$. Next, several (or $0$) SQL statements will be given. All SQL statements before the next $\tt commit()$ or $\tt abort()$ statement belong to this transaction. It is guaranteed that the next **transaction statement** must be $\tt commit()$ or $\tt abort()$. **2.2.** $\tt commit$ **Statement** **Syntax** $\tt commit()$ **Explanation** All changes to the database caused by the SQL statements between the previous $\tt begin()$ statement and this statement need to be preserved. If this statement is not the last statement, it is guaranteed that the next statement must be $\tt begin()$. **2.3.** $\tt abort$ **Statement** **Syntax** $\tt abort()$ **Explanation** All changes to the database caused by the SQL statements between the previous $\tt begin()$ statement and this statement need to be discarded. If this statement is not the last statement, it is guaranteed that the next statement must be $\tt begin()$.

输入格式

The first line contains two integers $n (1 \leq n \leq 1000),q(3 \leq q \leq 3000)$, representing the number of field names and the number of statements, respectively. The second line contains $n$ strings $key_1, \dots, key_n$, representing the field names. Each field name is guaranteed to contain only lowercase and uppercase letters, numbers, and underscores, and the total length of all field names does not exceed $5000$. The next $q$ lines each contain a string representing the statement to be executed. Each statement satisfies the following conditions: $\cdot$ The length does not exceed $2000$; $\cdot$ The format strictly complies with the syntax constraints mentioned above; $\cdot$ The number of left parentheses \`$\tt ($' does not exceed $10$.

输出格式

**For each statement starting with** $\tt select$ **or** $\tt select\_in$**:** The first line outputs an integer $x$, representing the number of entries in the returned set. Next: $\cdot$ If $x=0$, then you do not need to output anything for this statement; $\cdot$ If $x\geq 1$, to reduce the amount of output, you only need to output three lines: the first line is the first result, the second line is the $\lceil\frac{x}{2}\rceil$\-th result, and the third line is the $x$\-th result. **For each statement starting with** $\tt delete$ **or** $\tt delete\_in$**:** Only output one line containing an integer, representing the number of entries deleted.

输入样例 复制

3 13
id name birth_place
begin()
insert(a,Alice,Beijing)
insert(b,Bob,Beijing)
insert(c,Yangqishaonian,Nowcoder)
select(name,birth_place,Beijing)
commit()
begin()
delete(birth_place,Beijing)
select(name,birth_place,Beijing)
abort()
begin()
select(name,birth_place,Beijing)
abort()

输出样例 复制

2
Alice
Alice
Bob
2
0
2
Alice
Alice
Bob

分类标签