题目
实验二 括号匹配判断算法
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 4428 Accepted: 1693
Description
假设在表达式中([]())或[([ ][ ])]等为正确的格式,[( ])或([( ))或 (( )])均为不正确的格式。基于栈设计一个判断括号是否正确匹配的算法。
Input
输入数据有多组,每组测试数据一行,一个字符串(长度小于1000),只包含 ‘[‘ ,’]’ ,’(‘ ,‘)’ 。
Output
对于每组测试数据,若括号匹配输出yes,否则输出 no。
Sample Input
[]()[]
([[]])[)
Sample Output
yes
no
http://10.7.95.2/DataStruct/showproblem?problem_id=1002
(需使用校VPN)
算法分析
基本思路:
利用进栈与出栈的方式
实现对左括号的判断进栈
当遇到右括号的时候进行出栈
关于栈的基本建立
栈的长度|建立|与|增加|预设值
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
输出定义return处
#define OK 1
#define OVERFLOW -2
#define ERROR 0
栈结构体
typedef char SElemType;
typedef int Status;
typedef struct {
ElemType *base; //栈底指针,栈构造前和销毁后为空
ElemType *top; //栈顶指针,指向栈顶元素的下一位置
int stacksize; //当前分配的栈的存储空间数
}SqStack;
初始化创建
Status InitStack(SqStack &S)
{
S.base = (SElemType *)malloc(STACK_INIT_SIZE*sizeof(SElemType));//分配空间
if(!S.base) exit(OVERFLOW);//代码健壮性检验
S.top= S.base;//栈顶=栈尾------初值
S.stacksize = STACK_INIT_SIZE;//栈长初始化
return OK;
}
栈空判断,为后边出栈作检验
Status StackEmpty(SqStack S)
{
return S.top == S.base;//栈空时候头为尾
}
压栈
Status Push(SqStack &S, SElemType e)
{
if(S.top - S.base>=S.stacksize)
return ERROR;//健壮性检验
*S.top++ = e;//赋值语句
return OK;
}
出栈
Status Pop(SqStack &S, SElemType &e)//参变量,栈S,输入值e
{
if(S.base == S.top) return ERROR;
e = *--S.top;//e传值
return OK;
}
Status Match(char *st)
{
SqStack S;//定义一个栈
InitStack(S);//初始化
SElemType e;//元素e
int flag = 1,i = 0;
while(st[i])//数组有值
{
switch(st[i])
{
case '(':
case '[':
case '{':
Push(S,st[i]);//左括号压栈
break;
case ')':
case '}':
case ']':
if(StackEmpty(S))//如果栈是空的话,flag=0
{
flag = 0;
break;
}
Pop(S,e);//出栈
if(!(e == '('&&st[i] == ')'||e == '['&&st[i] == ']'||e == '{'&&st[i] == '}'))//出栈e传值与输入匹配--match
{
flag = 0;
break;
}
}
if(!flag) break;
i++;//i指向下一个
}
if(flag && StackEmpty(S))
return true;//栈全空且flag=1
else
return false;
}
源代码
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
#define OK 1
#define OVERFLOW -2
#define ERROR 0
typedef char SElemType;
typedef int Status;
typedef struct{
SElemType *base;
SElemType *top;
int stacksize;
}SqStack;
Status InitStack(SqStack &S)
{
S.base = (SElemType *)malloc(STACK_INIT_SIZE*sizeof(SElemType));
if(!S.base) exit(OVERFLOW);
S.top= S.base;
S.stacksize = STACK_INIT_SIZE;
return OK;
}
Status StackEmpty(SqStack S)
{
return S.top == S.base;
}
Status Push(SqStack &S, SElemType e)
{
if(S.top - S.base>=S.stacksize)
return ERROR;
*S.top++ = e;
return OK;
}
Status Pop(SqStack &S, SElemType &e)
{
if(S.base == S.top) return ERROR;
e = *--S.top;
return OK;
}
Status Match(char *st)
{
SqStack S;
InitStack(S);
SElemType e;
int flag = 1,i = 0;
while(st[i])
{
switch(st[i])
{
case '(':
case '[':
case '{':
Push(S,st[i]);
break;
case ')':
case '}':
case ']':
if(StackEmpty(S))
{
flag = 0;
break;
}
Pop(S,e);
if(!(e == '('&&st[i] == ')'||e == '['&&st[i] == ']'||e == '{'&&st[i] == '}'))
{
flag = 0;
break;
}
}
if(!flag) break;
i++;
}
if(flag && StackEmpty(S))
return true;
else
return false;
}
int main(){
char st[1000];
int i = 0;
while(gets(st))
{
if(Match(st)) printf("yes\n");
else printf("no\n");
}
return 0;
}