括号配对问题

括号配对问题

时间限制:3000 ms  |  内存限制:65535 KB
难度:3
描述
现在,有一行括号序列,请你检查这行括号是否配对。
输入
第一行输入一个数N(0<N<=100),表示有N组测试数据。后面的N行输入多组输入数据,每组输入数据都是一个字符串S(S的长度小于10000,且S不是空串),测试数据组数少于5组。数据保证S中只含有"[", "]", "(", ")" 四种字符
输出
每组输入数据的输出占一行,如果该字符串中所含的括号是配对的,则输出Yes,如果不配对则输出No
样例输入
3
[(])
(])
([[]()])
样例输出
No
No
Yes

题目转自南阳理工学院:http://acm.nyist.edu.cn/JudgeOnline/problemset.php


个人代码(以下原创)


#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <cstring>

char a[10000], b[10000];

int main()
{
	int n;
	scanf("%d", &n);
	getchar();
	while (n--)
	{
		gets(a);
		int m = strlen(a);
		int count = -1;
		for (int i = 0; i < m; i++)
		{
			if (strlen(b) == -1)
			{
				b[count+1] = a[i];
			}
			else
			{
				if (a[i] == ']' && b[count] == '[')
				{
					b[count] = 0;
					count--;
				}
				else
				{
					if (a[i] == ')' && b[count] == '(')
					{
						b[count] = 0;
						count--;
					}
					else
					{
						b[count+1] = a[i];
						count++;
					}
				}
				
			}
		}
		if (count == -1)
		{
			printf("Yes\n");
		}
		else
		{
			printf("No\n");
		}
	}

	return 0;
}


posted @ 2018-04-07 18:38  focus5679  阅读(85)  评论(0编辑  收藏  举报