作业帮 > 英语 > 作业

ACM 括号匹配问题,为什么一定要用栈来写?

来源:学生作业帮 编辑:作业帮 分类:英语作业 时间:2024/05/14 15:24:36
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】不等于】或者)
可是这样子提交上去就是运行错误.去看答案说是要用栈,我明白栈的原理可是这个方法不行吗?为何一定要用他?
用栈写比较简单呀,其他方法要考虑很多~你这种思路没考虑周全,反例一大堆.我随便举两个,如1、())( 2、()][
再问: 明白了!对了能再问你一个问题吗,就是每次我都会被所谓的结束方式给纠结,题目如果没说怎么结束程序的话我就很蛋疼!!=EOF或者!=NULL什么的我都搞乱了
再答: 一般都用EOF,就是一直输入到文件结束,,有些输入字符的才用NULL,具体的你百度或google吧。。。