Skip to content

Commit 5ec6ff0

Browse files
committed
feat: init 2025fall/lab-4 done
Signed-off-by: Certseeds <51754303+Certseeds@users.noreply.github.com>
1 parent 688c5b6 commit 5ec6ff0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

77 files changed

+881
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
## Description
2+
3+
There are $n$ brackets, and you want to know whether they are matched.
4+
5+
The brackets will only contain $\{,\},(,),[,]$.
6+
7+
Here is the matching rules.
8+
9+
For example, {{[}]} is not matching, but [{{}}()] is matching.
10+
11+
Please write a program to check whether the given string is matched or not.
12+
13+
## Input
14+
15+
The first line will be an integer $T$, which is the number of test cases. $(1 \leq T \leq 10)$
16+
17+
For each test case, the first line will be an integer $n$ $(1 \leq n \leq 30000)$
18+
19+
Then there is a line with $n$ brackets.
20+
21+
## Output
22+
23+
For each test case, print YES if the test case is a matching case.
24+
25+
Otherwise, print NO.
26+
27+
## Sample Input
28+
29+
``` log
30+
7
31+
1
32+
(
33+
2
34+
()
35+
2
36+
{]
37+
6
38+
[(){}]
39+
4
40+
(])[
41+
8
42+
[[{{}}]]
43+
6
44+
[][{]]
45+
```
46+
47+
## Sample Output
48+
49+
``` log
50+
NO
51+
YES
52+
NO
53+
YES
54+
NO
55+
YES
56+
NO
57+
```
58+
59+
### 算法分析
60+
61+
本题采用栈结构判断括号序列是否合法. 遍历字符串, 遇到左括号入栈, 遇到右括号判断栈顶是否为对应类型的左括号, 若匹配则弹栈, 否则不合法. 最终栈为空则匹配成功, 否则失败.
62+
63+
该算法时间复杂度为 $O(n)$, 空间复杂度为 $O(n)$.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<parent>
7+
<groupId>nanoseeds.algorithm-template</groupId>
8+
<artifactId>2025fall-lab-04</artifactId>
9+
<version>${revision}</version>
10+
<relativePath>./../pom.xml</relativePath>
11+
</parent>
12+
<groupId>nanoseeds.algorithm-template.2025fall-lab-04</groupId>
13+
<artifactId>2025-04-Brackets-Matching</artifactId>
14+
<version>${revision}</version>
15+
<name>${project.groupId}.${project.artifactId}</name>
16+
<description>${project.groupId}.${project.artifactId}</description>
17+
18+
<build>
19+
<sourceDirectory>${project.basedir}/src</sourceDirectory>
20+
<testSourceDirectory>${project.basedir}/test</testSourceDirectory>
21+
</build>
22+
</project>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
7
2+
1
3+
(
4+
2
5+
()
6+
2
7+
{]
8+
6
9+
[(){}]
10+
4
11+
(])[
12+
8
13+
[[{{}}]]
14+
6
15+
[][{]]
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
NO
2+
YES
3+
NO
4+
YES
5+
NO
6+
YES
7+
NO
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
1
2+
1
3+
(
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
NO
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
1
2+
2
3+
{]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
NO
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
1
2+
512
3+
(((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((())))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
YES

0 commit comments

Comments
 (0)