CPE通靈大法

以CPE 2025/9/30 第三題為例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
You are given a string consisting of parentheses () and []. A string of this type is said to be correct:

(a) if it is the empty string

(b) if A and B are correct, AB is correct,

(c) if A is correct, (A) and [A] is correct.

Write a program that takes a sequence of strings of this type and check their correctness. Your program can assume that the maximum string length is 128.

Input
The file contains a positive integer n and a sequence of n strings of parentheses ‘()’ and ‘[]’, one string a line.

Output

A sequence of ‘Yes’ or ‘No’ on the output file.

Sample Input
3

([])

(([()])))

([()[]()])()

Sample Output
Yes

No

Yes

機哩瓜拉說啥呢,直接看測資。
我在考試的時候發現如果()or[]的數量不匹配就會No,像二個的)多了。
然後我就那樣寫,然後有兩個錯(人工)。
我就滑到最下面,喔原來是最後一個為空,那就對他特別處理。
然後再看另外一個,我記得是第二十二個,把他拉上來(刪前面的)。
我發現說他最後一個是(,所以我就對他去做特別處理,如果最後一個為(或[就No,這樣。
然後就AC了。 舒服