括号匹配(顺序表)
*
/Decripsitno
1、问题描
述一算术表个达中包括式圆括、号括方和花括号三号形式种括的号
编实现判别程表式中达号括否是正匹配的算确法
2
、算法
序扫顺算术描表达式
若算术达表式扫完成,此时描如栈果空,则确返回正0(;如果栈)空,说未明括左多号于括右,返号(回3)
从-术算达表式取出一中字符个,如是果括号(‘左(‘或[‘或‘‘ ‘{,则让)该号进括(栈USHP)如果
是右括号()‘‘‘]或或‘‘} )‘:⑴、如果栈为空,则说
右括明号多于左号,返回括(2-)
⑵如、果栈为空不则从,栈弹顶出P(O)一个P号: 括若号匹配括则,转1续进行继断判;则,否说明右括左配对次号序正不确,返(-回1)
Input
第一:行本样个数假设,为。n
第到二+n1,每行一行是一个样(本算表术达串式)共n,个试测本样
。
uOtutp
有共行n,一每行是个测试结果一有四,结果种:0
左:括右号匹配正确 [(1+2)*{]-31}
-:左右括1配对次序不号确 {正[(1+2]3*-1})
2:右-号括多左括于号 (12)*+3)-1
}3:-左号多括于括号 右 [(1{2+)3-*]1
aSpmelI nput
4
[(1{2)+3]*1-}{[(1+
2]*)-31}(1+
)2*3-1)
{}([12+)*-31]S
apml Outpetu
-01
-2-3
*
/
inc#udleia#
iclnueh
usnig amnspeaces td;
i
t watchnc(arh[ )]
v;iod USP(cHha rh)c;
hcr PaOP();
ntifu nc(ar hac,ahrb) ;
hac rs[200,]ie[3l]{={'''[,,'('},'hag[3n]{'}='',',')]'};
itn bae=0s,to=0p;
ni tmpa3][3[]=1,0,{0,
01,0,,
0,0,1}
;int main(
){
car shr[t20];0
ni n;
t
/ /feorpe("nin.cxtt,"""rstdi,n;
)cin>n;>
while(n-)-
{
in>>ctr;s
cou
ae=s;
0ot=p0;
}
r
etrn 0u;}
intwa tc(chha rhc[]){
int
=i;0
while(c[i]h=!'\')0
if({c[hi]=={'|'|ch[]=i'=[|'ch|i]=['=(')P
UH(Sch[i])
;
i(fhc[]=='}'|ic|hi][==]''|c|h[]==i')'
){c
hrag ethcr=PaOP(;)
f(ietcharg==''#)
etrur n2-
;lee sif(unf(getcar,hc[ih)])re
urnt- 1;
}i
+;+}
if(POP(
)==#'')reutrn0;
el
e resurt n-3
;
}
ovd iUSHP(harcch)
s{top[=c];
hotp+;+
}
chr aOP(P)
{if
(aseb==op)return 't#';
el
es
{
tpo--
retu;n r[top];s }
}
in
t fn(cuha rac,ah br)
i{nti ,;j
for(=0;i
)fi(ieli]=[=a
{
fo)(jr0=;
fi(anhgj][=b)=
fii=(j)=retrnu 0;el
se ertrnu1
;
}
}