ACM 括号匹配问题,为什么一定要用栈来写?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) an

来源:学生作业帮助网 编辑:作业帮 时间:2024/11/08 06:02:27

ACM 括号匹配问题,为什么一定要用栈来写?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) an
ACM 括号匹配问题,为什么一定要用栈来写?
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.
Sample Input
3
([])(([()])))([()[]()])()Sample Output
Yes
No
Yes
我是新人,打算定义四个变量K1K2K3K4,分别对应()【】,检测完一个字符串后比较K1是否等于K2,K3是否等于K4,同时前提条件是a【0】不等于】或者)
可是这样子提交上去就是运行错误.去看答案说是要用栈,我明白栈的原理可是这个方法不行吗?为何一定要用他?

ACM 括号匹配问题,为什么一定要用栈来写?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) an
用栈写比较简单呀,其他方法要考虑很多~你这种思路没考虑周全,反例一大堆.我随便举两个,如1、())( 2、()][