括号平衡 uva673

#include <stdio.h>
#include <string.h>

char s[128] = {0};
char stack[128] = {0};
int stackFloorNum = 0;

void push(char a)
{
	stackFloorNum++;
	stack[stackFloorNum-1] = a;
	
	return;	
}

void pop()
{
	stack[stackFloorNum-1] = 0;
	stackFloorNum--;
	
	return;	
}

int match(char a, char b)
{
	if((a== '(' && b == ')')
	     || (a== '[' && b == ']'))
	{
		return 1;	
	}
	else
	{
		return 0;
	}
}

int main(int argc, char *argv[])
{
	int n = 0;
	int i = 0;
	int j = 0;
	scanf("%d", &n);
	
	for(i = 0; i < n; i++)
	{
		scanf("%s", s);
		
		push(s[0]);
		for(j = 1; j < strlen(s); j++)
		{
			if(match(stack[stackFloorNum-1], s[j]))
			{
				pop();
			}	
			else
			{
				push(s[j]);	
			}
		}
		
		if(stackFloorNum > 0)
		{
			printf("No\n");
		}
		else{
			printf("Yes\n");
		}
	}

	return 0;
}

  

posted @ 2014-01-23 10:43  reasontom  阅读(180)  评论(0编辑  收藏  举报