简述
最近初学算法,从最基础的开始。看到算法书上提到了括号匹配问题,并没有给出解答。就自己摸索写了一下,用的是类似栈的形式,但还没有用到push和pop,看起来比较好懂一点。
C代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 100
struct Parenthesis
{
char paren[MAX];
int top;
};
int main()
{
struct Parenthesis str;
scanf("%s",str.paren);
str.top = strlen(str.paren) - 1;
char tmp[MAX];
int i=0;
while(str.top != -1)
{
tmp[i] = str.paren[str.top];
switch(tmp[i])
{
case '(':
if (tmp[i-1]==')') i-=2;break;
case '[':
if(tmp[i-1]==']') i-=2;break;
case '{':
if(tmp[i-1]=='}') i-=2;break;
}
i++;
str.top--;
}
if (i)
printf("No\n");
else
printf("Yes\n");
return 0;
}