acm括号配对问题c语言,ACM:UESTC - 649 括号配对问题 - stack

/*/

简单的括号配对问题

先把给出的数字串全部变成括号串,在用栈去压入括号,每次保存栈顶括号,如果栈顶括号能和下一个括号配对,就把栈顶弹出,指括号的指针后移。

最后判断栈内是否还有元素,如果有,就说明配对不成功。

AC代码:

/*/

#include"algorithm"

#include"iostream"

#include"cstring"

#include"cstdlib"

#include"cstdio"

#include"string"

#include"vector"

#include"stack"

#include"queue"

#include"cmath"

#include"map"

using namespace std;

typedef long long LL ;

#define lson l,m,rt<<1

#define rson m+1,r,rt<<1|1

#define FK(x) cout<

#define memset(x,y) memset(x,y,sizeof(x))

#define memcpy(x,y) memcpy(x,y,sizeof(x))

#define bigfor(T) for(int qq=1;qq<= T ;qq++)

const int MX=1e5+1e3;

char s[MX],cc[MX],t[MX];

stack st;

bool check() {

int len = strlen(cc);

while(!st.empty())st.pop();

int i=0;

char stc;

while(i

st.push(cc[i++]);

stc=st.top();

while(stc=='('&&cc[i]==')'){

st.pop();

i++;

if(st.empty())break;

stc=st.top();

}

}

if(st.empty())return 0;

return 1;

}

int main() {

int T;

t[0]='(',t[1]=')',t[2]='\0';

scanf("%d",&T);

bigfor(T) {

scanf("%s",s);

int len=strlen(s);

int erear=0;

for(int i=0; i

if(s[i]=='('||s[i]==')') {

cc[erear++]=s[i];

}

}

cc[erear]='\0';

if(check()) {

puts("No");

} else puts("Yes");

}

return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值