用栈,如果当前的字符是反括号()、] 或 >),并且刚好是栈顶反过来,就匹配了一对括号,弹出栈顶,否则把当前的字符压入栈。最后栈空就是匹配。
Code
12345678910111213141516171819202122232425262728293031323334353637383940414243444546#include<bits/stdc++.h>#defineintlonglong#definerep(i,a,b)for(inti(a);i<=(b);++i)#definereq(i,a,b)for(inti(a);i>=(b);--i)usingnamespacestd;charbuf[1<<23],*p1=buf,*p2=buf,ubuf[1<<23],*u=ubuf;#definegetchar()(p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)template<typenameTP>inlineTPread(TP&num){TPx=0;intf=0;charch=getchar();while(ch<48||ch>57)f|=ch==’-’,ch=getchar();while(48<=ch&&ch<=57)x=(x<<1)+(x<<3)+(ch^48),ch=getchar();returnnum=f?-x:x;}template<typename...Args>inlinevoidread(Args&...args){(read(args),...);}template<typenameTP>inlinevoidwrite(TPx){(x<0)?(putchar(’-’),x=-x):0;(x>9)?(write(x/10),0):0;putchar((x%10)^48);}template<typenameTP>inlinevoidwriteln(TPx){write<TP>(x);puts("");}strings;intn;inlinecharcvt(chars){returns==’(’?’)’:s==’<’?’>’:’]’;}signedmain(){cin>>s;n=s.size();stack<char>S;rep(i,0,n-1){if(!S.empty()&&cvt(S.top())==s[i])S.pop();elseS.push(s[i]);}if(S.empty())puts("Yes");elseputs("No");return0;}