673 - Parentheses Balance

简单括号匹配问题

#include <iostream>
#include <string>
#include <cstring>
#include <cstdio>
using namespace std;
int main()
{
	int n;
	scanf("%d",&n);
	getchar();
	char stack[130];
	int top;
	for(int cas=0;cas<n;cas++)
	{
		memset(stack,0,sizeof(stack));
		top=0;
		string line;
		getline(cin,line);//这里出错,题中明确说明one string a line
		int i;
		for(i=0;i<line.size();i++)
		{
			if(line[i]=='(' || line[i]=='[')
				stack[top++]=line[i];
			else if(line[i]==')')
			{
				if(top==0 || stack[--top]!='(')
					break;
			}
			else if(line[i]==']')
			{
				if(top==0 || stack[--top]!='[')
					break;
			}
		}
		if(i==line.size() && top==0)
			printf("Yes\n");
		else 
			printf("No\n");
	}
	return 0;
}



你可能感兴趣的:(673 - Parentheses Balance)