数据结构-NOJ-07-表达式括号匹配


这道题。。。卡了我好长时间,再次认识到了C++的功底不足...

此篇文章讲述的是编译器的编译时间的问题。此题时间复杂度O(n)

由于一些细节的原因而造成了编译器效率低下,调试的时候卡的跟吃了翔一样,求提交代码时一直TE的心理阴影面积TAT

首先上错误代码(效率低下,没有解决问题):

#include <iostream>
#include<math.h>
#include<stdlib.h>
using namespace std;

#define MAXSIZE 100+10
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0

typedef int Status;
typedef char SElemType;

typedef struct
{
    SElemType *base;
    SElemType *top;
    int stacksize;
}Stack;

void InitStack(Stack &S)
{
    S.base = new SElemType[MAXSIZE];
    if(!S.base)
        exit(OVERFLOW);
    S.top=S.base;
    S.stacksize=MAXSIZE;
}



Status StackEmpty(Stack S)
{
    if(S.top==S.base)
        return TRUE;
    else
        return FALSE;
}




Status GetTop(Stack S)
{
    if(S.top != S.base)
        return *(S.top-1);
    else
        return ERROR;
}


Status push(Stack &S,SElemType e)
{
    if(S.top-S.base >= S.stacksize)
    {
        S.base=(SElemType*)realloc(S.base,(S.stacksize+10)*sizeof(SElemType));
        if(!S.base)
            return FALSE;
        S.top=S.base+MAXSIZE;
        S.stacksize=S.stacksize+10;
    }
    *S.top++=e;
    return OK;
}

Status Pop(Stack &S,SElemType e)
{
    if(S.top==S.base)
        return ERROR;
    else
        e=*--S.top;
    return OK;
}



void Matching()
{
    Stack S;
    SElemType ch;
    SElemType x;
    InitStack(S);
    int flag=1;
    cin>>ch;
    while(ch!='\0'&&flag)      
    {
        switch(ch)
        {
        case '[':
        case '(':
        case '{':
            push(S,ch);
            break;
        case '}':
            if(!StackEmpty(S)&&GetTop(S)=='{')
                Pop(S,x);
            else
                flag=0;
            break;
        case ')':
             if(!StackEmpty(S)&&GetTop(S)=='(')
                Pop(S,x);
            else
                flag=0;
            break;
        case ']':
             if(!StackEmpty(S)&&GetTop(S)=='[')
                Pop(S,x);
            else
                flag=0;
            break;
        }
        cin>>ch;
    }
    if(StackEmpty(S)&&flag)
        cout<<"yes";
    else
        cout<<"no";
}

int main()
{
    Matching();
    return 0;
}

就是这个ch!=' '问题,判断字符型需要好久的,同'#','\n',etc.改成'\0'时间大大缩短。

然而并没有解决问题

这里采用转换成数组的方法,判断数组的元素ch[i] !='\0'

然后是正确的:

#include <iostream>
#include<math.h>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
using namespace std;

#define MAXSIZE 100+10
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0

typedef int Status;
typedef char SElemType;

typedef struct
{
    SElemType *base;
    SElemType *top;
    int stacksize;
}Stack;

void InitStack(Stack &S)
{
    S.base = new SElemType[MAXSIZE];
    if(!S.base)
        exit(OVERFLOW);
    S.top=S.base;
    S.stacksize=MAXSIZE;
}



Status StackEmpty(Stack S)
{
    if(S.top==S.base)
        return TRUE;
    else
        return FALSE;
}




Status GetTop(Stack S)
{
    if(S.top != S.base)
        return *(S.top-1);
    else
        return ERROR;
}


Status push(Stack &S,SElemType e)
{
    if(S.top-S.base >= S.stacksize)
    {
        S.base=(SElemType*)realloc(S.base,(S.stacksize+10)*sizeof(SElemType));
        if(!S.base)
            return FALSE;
        S.top=S.base+MAXSIZE;
        S.stacksize=S.stacksize+10;
    }
    *S.top++=e;
    return OK;
}

Status Pop(Stack &S,SElemType &e)             //引用e改变其值
{
    if(S.top==S.base)
        return ERROR;
    else
        e=*--S.top;
    return OK;
}



void Matching(SElemType *ch)
{
    Stack S;
    SElemType x;
    InitStack(S);
    int flag=1;
    int i=0;
    while(ch[i]!='\0'&&flag)
    {
        switch(ch[i])
        {
        case '[':
        case '(':
        case '{':
            push(S,ch[i]);
            break;
        case '}':
            if(!StackEmpty(S)&&GetTop(S)=='{')
                Pop(S,x);
            else
                flag=0;
            break;
        case ')':
             if(!StackEmpty(S)&&GetTop(S)=='(')
                Pop(S,x);
            else
                flag=0;
            break;
        case ']':
             if(!StackEmpty(S)&&GetTop(S)=='[')
                Pop(S,x);
            else
                flag=0;
            break;
        }
        i++;
    }
    if(StackEmpty(S)&&flag)
        cout<<"yes";
    else
        cout<<"no";
}

int main()
{
    SElemType ch[MAXSIZE];
    memset(ch,0,sizeof(ch));
    gets(ch);
    Matching(ch);
    return 0;
}
 

在执行pop操作的时候,因为要对栈里的值进行修改,所以要引用变量e 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值