这道题。。。卡了我好长时间,再次认识到了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