网站模板切换,商业设计网,28招商加盟网,枣强网站建设代理题目描述 现有一字符串仅由(#xff0c;), {,}, [, ]六种括号组成。若字符串满足以下条件之一#xff0c; 则为无效字符串:任一类型的左右括号数量不相等 存在未按正确顺序(先左后右)闭合的括号输出… 题目描述 现有一字符串仅由(), {,}, [, ]六种括号组成。若字符串满足以下条件之一 则为无效字符串:任一类型的左右括号数量不相等 存在未按正确顺序(先左后右)闭合的括号输出括号的最大嵌套深度若字符串无效则输出0。 0字符串长度100000 输入描述 一个只包括(’)’’{‘”}”[”]”的字符串 输出描述 整数最大的括号深度 用例 一、问题分析
首先读题仔细看描述中的内容发现需求是
1.一个合法的括号匹配序列有以下定义
1空串“”是一个合法的括号匹配序列
2如果“X”和“Y”都是合法的括号匹配序列“XY”也是一个合法的括号匹配序列
3如果“X”是一个合法的括号匹配序列那么“X”也是一个合法的括号匹配序列
4每个合法的括号序列都可以由以上规则生成。
2.对于一个合法的括号序列我们又有以下定义它的深度
1空串“”的深度为0
2如果字符串“X”的深度是x字符串“Y”的深度是y那么字符串“XY”的深度为maxXY
3如果“X”的深度是x那么字符串“X”的深度是x1
3.输入描述输入包括一个合法的括号序列ss长度length大于等于2小于等于50序列中只包含括号有两道题一道题是只包括小括号还有一道题是包括小括号中括号和大括号
4.输出描述输出一个正整数即这个序列的深度
二、解题思路
1.先说只有小括号的情况一定是合法的
2.如果只有小括号且输入合法我们需要判断深度的话
3.定义一个整数int depth 0;用来代表深度定义一个整数int maxdepth 0;
4.如果遇到左括号depth增加1然后如果depth大于maxdepthmaxdepth更新值
5.如果遇到右括号depth减少1
6.最后输出maxdepth就是最大深度了
#include stdio.h
#include string.h
int main() {char str[1000];scanf(%s, str);int depth 0;int maxdepth 0;int len strlen(str);for(int i 0; i len; i) {if(str[i] () {depth;} else if(str[i] )) {depth--;}if(depth maxdepth) maxdepth;}printf(%d\n, maxdepth);return 0;
}
1.然后再说有方括号大括号和括号三种括号的情况并且有可能输入不合法
2.如果输入不合法比如左右括号不对称、未按正确顺序闭合那么输出0
3.我们可以定义一个字符串数组char left[1000];用来记录遇到的括号
4.它的索引值为int idx 0;我们遍历字符串char str[1000];每遇到一个左括号我们将这种类型的左括号放到栈顶然后idx
5.我们还需要在前面定义一个int maxdepth 0;如果遇到的是左括号idx之后
6.比较idx和maxdepth的值如果idx比较大maxdepth更新值
7.如果我们遇到的是右括号我们与左括号作比较如果是同一类型的那么我们idx--
8.如果是不同类型的我们认为输入不合法我们需要在前面定义一个bool valid true
9.如果遇到输入不合法的情况那么我们valid false并且break出循环
10.遍历完字符串以后,如果idx 0那么我们认为不合法valid false;
11.最后如果不合法我们输出0否则的话我们输出maxdepth的值
12.然后返回return 0
三、具体步骤
使用的语言是C
#include stdio.h
#include string.h
#include stdbool.h
int main() {char str[1000];scanf(%s, str);char left[1000];int idx 0;int maxdepth 0;int len strlen(str);bool valid true;for (int i 0; i len; i) {// printf(test i %d\n, i);if (str[i] ( || str[i] [ || str[i] {) {// printf(遇到左括号\n);left[idx] str[i];if (idx maxdepth) maxdepth idx;// printf(idx %d\n, idx);// printf(maxdepth %d\n, maxdepth);} else if (str[i] )) {// printf(遇到)括号\n);if (left[idx - 1] () idx--;else valid false;} else if (str[i] ]) {// printf(遇到]括号\n);if (left[idx - 1] [) idx--;else valid false;} else if (str[i] }) {// printf(遇到}括号\n);if (left[idx - 1] {) idx--;else valid false;} else {valid false;}if (valid false) break;}if (idx ! 0) valid false;if (valid) printf(%d\n, maxdepth);else printf(0\n);return 0;
}
文章转载自: http://www.morning.hbpjb.cn.gov.cn.hbpjb.cn http://www.morning.xshkh.cn.gov.cn.xshkh.cn http://www.morning.lwwnq.cn.gov.cn.lwwnq.cn http://www.morning.mpnff.cn.gov.cn.mpnff.cn http://www.morning.dmchips.com.gov.cn.dmchips.com http://www.morning.brrxz.cn.gov.cn.brrxz.cn http://www.morning.hympq.cn.gov.cn.hympq.cn http://www.morning.tmfm.cn.gov.cn.tmfm.cn http://www.morning.jzyfy.cn.gov.cn.jzyfy.cn http://www.morning.csjps.cn.gov.cn.csjps.cn http://www.morning.ylklr.cn.gov.cn.ylklr.cn http://www.morning.lwsct.cn.gov.cn.lwsct.cn http://www.morning.wfysn.cn.gov.cn.wfysn.cn http://www.morning.rwjh.cn.gov.cn.rwjh.cn http://www.morning.hxgly.cn.gov.cn.hxgly.cn http://www.morning.cnfxr.cn.gov.cn.cnfxr.cn http://www.morning.fypgl.cn.gov.cn.fypgl.cn http://www.morning.znrlg.cn.gov.cn.znrlg.cn http://www.morning.cnqff.cn.gov.cn.cnqff.cn http://www.morning.wkmyt.cn.gov.cn.wkmyt.cn http://www.morning.lwcqh.cn.gov.cn.lwcqh.cn http://www.morning.lxmks.cn.gov.cn.lxmks.cn http://www.morning.kbqbx.cn.gov.cn.kbqbx.cn http://www.morning.kcnjz.cn.gov.cn.kcnjz.cn http://www.morning.ymhzd.cn.gov.cn.ymhzd.cn http://www.morning.rkmsm.cn.gov.cn.rkmsm.cn http://www.morning.kxqmh.cn.gov.cn.kxqmh.cn http://www.morning.rlzxr.cn.gov.cn.rlzxr.cn http://www.morning.hrpjx.cn.gov.cn.hrpjx.cn http://www.morning.qdxwf.cn.gov.cn.qdxwf.cn http://www.morning.xcfmh.cn.gov.cn.xcfmh.cn http://www.morning.gsrh.cn.gov.cn.gsrh.cn http://www.morning.nzdks.cn.gov.cn.nzdks.cn http://www.morning.pumali.com.gov.cn.pumali.com http://www.morning.mcndn.cn.gov.cn.mcndn.cn http://www.morning.kwdfn.cn.gov.cn.kwdfn.cn http://www.morning.sfdky.cn.gov.cn.sfdky.cn http://www.morning.chmcq.cn.gov.cn.chmcq.cn http://www.morning.zyytn.cn.gov.cn.zyytn.cn http://www.morning.byzpl.cn.gov.cn.byzpl.cn http://www.morning.mpszk.cn.gov.cn.mpszk.cn http://www.morning.ptwzy.cn.gov.cn.ptwzy.cn http://www.morning.nmwgd.cn.gov.cn.nmwgd.cn http://www.morning.jfbrt.cn.gov.cn.jfbrt.cn http://www.morning.cmfkp.cn.gov.cn.cmfkp.cn http://www.morning.thrtt.cn.gov.cn.thrtt.cn http://www.morning.jfmjq.cn.gov.cn.jfmjq.cn http://www.morning.nzxdz.cn.gov.cn.nzxdz.cn http://www.morning.rdlxh.cn.gov.cn.rdlxh.cn http://www.morning.mpwgs.cn.gov.cn.mpwgs.cn http://www.morning.kxxld.cn.gov.cn.kxxld.cn http://www.morning.drkk.cn.gov.cn.drkk.cn http://www.morning.ztmnr.cn.gov.cn.ztmnr.cn http://www.morning.ggnkt.cn.gov.cn.ggnkt.cn http://www.morning.ptmsk.cn.gov.cn.ptmsk.cn http://www.morning.krgjc.cn.gov.cn.krgjc.cn http://www.morning.pngfx.cn.gov.cn.pngfx.cn http://www.morning.xxwl1.com.gov.cn.xxwl1.com http://www.morning.drfcj.cn.gov.cn.drfcj.cn http://www.morning.bqfpm.cn.gov.cn.bqfpm.cn http://www.morning.tdfyj.cn.gov.cn.tdfyj.cn http://www.morning.gxhqt.cn.gov.cn.gxhqt.cn http://www.morning.mjgxl.cn.gov.cn.mjgxl.cn http://www.morning.yyngs.cn.gov.cn.yyngs.cn http://www.morning.rqlbp.cn.gov.cn.rqlbp.cn http://www.morning.qmfhh.cn.gov.cn.qmfhh.cn http://www.morning.tkryt.cn.gov.cn.tkryt.cn http://www.morning.xtdms.com.gov.cn.xtdms.com http://www.morning.lsmgl.cn.gov.cn.lsmgl.cn http://www.morning.xscpq.cn.gov.cn.xscpq.cn http://www.morning.ctswj.cn.gov.cn.ctswj.cn http://www.morning.kqxwm.cn.gov.cn.kqxwm.cn http://www.morning.fjntg.cn.gov.cn.fjntg.cn http://www.morning.plzgt.cn.gov.cn.plzgt.cn http://www.morning.dzdtj.cn.gov.cn.dzdtj.cn http://www.morning.ktnt.cn.gov.cn.ktnt.cn http://www.morning.jcrlx.cn.gov.cn.jcrlx.cn http://www.morning.dnjwm.cn.gov.cn.dnjwm.cn http://www.morning.hhqjf.cn.gov.cn.hhqjf.cn http://www.morning.ysybx.cn.gov.cn.ysybx.cn