NYOJ-括号配对问题

括号配对问题
时间限制:3000 ms | 内存限制:65535 KB
难度:3
描述
现在,有一行括号序列,请你检查这行括号是否配对。
输入
第一行输入一个数N(0< N<=100),表示有N组测试数据。后面的N行输入多组输入数据,每组输入数据都是一个字符串S(S的长度小于10000,且S不是空串),测试数据组数少于5组。数据保证S中只含有”[“,”]”,”(“,”)”四种字符
输出
每组输入数据的输出占一行,如果该字符串中所含的括号是配对的,则输出Yes,如果不配对则输出No

样例输入

3
[(])
(])
([])

样例输出

No
No
Yes

AC

#include <stdio.h>
#include <string.h>
int main()
{
    int N,i,a[4],top;
    char S[10000];
    scanf("%d",&N);
    while(N--)
    {
        for(i=0;i<4;i++)
        {a[i]=0;}
        scanf("%s",S);
        top=0;
        if(strlen(S)%2!=0)
        {
            printf("No\n");
        }
        else
        {
            for(i=1;i<strlen(S);i++)
            {
                if(S[i]==']'&&S[top]=='[' || S[i]==')'&&S[top]=='(')//判断是否可以配对 
                {
                    top--;
                } 
                else
                {
                    top++;
                    S[top]=S[i];//覆盖之前已经配对完成的数据
                }
            }
            if(top==-1)//结果 
            {
                printf("Yes\n");
            }
            else
            {
                printf("No\n");
            }
        }
    } 
    return 0;
}

学习了栈之后,我使用栈重新做了一遍,可是一直RE,也没有在找原因,代码如下:

#include <stdio.h>
#include <string.h>
#include <stack>
using namespace std;
stack<char> osta;
char s[1000];
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int top=0,i;
        scanf("%s",s);
        int len=strlen(s);
        if(len%2!=0)//如果长度不是偶数,肯定不能配对啦
            printf("No\n");
        else
        {
            for(i=0;i<len;i++)
            {
                if(osta.empty())
                    osta.push(s[i]);//第一次输入将它放入栈
                else if(s[i]==']'&&osta.top()=='[')
                    osta.pop();
                else if(s[i]==')'&&osta.top()=='(')
                    osta.pop();
                else//如果是再一次输入[或(,再次将其放入栈
                    osta.push(s[i]);
            }
            if(osta.empty())
                printf("Yes\n");
            else
                printf("No\n");
            while(!osta.empty())//判断栈在判断完之后是否为空
                osta.pop();//清空栈
        }
    }
    return 0;
} 

测试之后又得到一种方法,原理也是栈
代码:

#include<stdio.h> 
#include<string.h> 
int main() 
{ 
    int T,i; 
    scanf("%d",&T); 
    char str[10010],ch[10010]; 
    while(T--) 
    { 
        memset(ch,0,sizeof(ch)); //清零
        memset(str,0,sizeof(str)); //清零!这点一定要有,否则会影响下一次
        scanf("%s",str); 
        int top=0; 
        for(i=0;i<strlen(str);i++) 
        { 
            if(str[i]=='['||str[i]=='('||str[i]=='{'||str[i]=='<') 
            { 
                ch[top++]=str[i]; //放入栈中
            } 
            else 
            { 
                if(ch[top-1]=='['&&str[i]==']'||ch[top-1]=='('&&str[i]==')'||ch[top-1]=='<'&&str[i]=='>'||ch[top-1]=='{'&&str[i]=='}') 
                { 
                    top--;   //出栈
                } 
                else
                { 
                   printf("No\n");  
                   break; //不能配对,直接退出
               } 
            } 
        } 
        if(top==0&&(i==strlen(str))) 
            printf("Yes\n"); 
    } 
    return 0; 
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值