菜菜

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

题意:

检测括号是否匹配,注意有空格

#include<stdio.h>
#include<iostream>
#include <strstream>
#include<string>
#include<memory.h>
#include<math.h>
#include<sstream>
#include<queue>
#include<stack>
using namespace std;
struct Node
{
	int r;
	int c;
	int total;
};
const Node dir[] = { { -1, 2 }, { 1, 2 }, { -2, 1 }, { 2, 1 }, { -2, -1 }, { 2,
		-1 }, { -1, -2 }, { 1, -2 } };

int main()
{

	string yes = "Yes";
	string no = "No";
	int n;
	cin >> n;
	string str;
	getline(cin, str);
	while (n--)
	{
		getline(cin, str);
		
		int length = str.length();
		stack<char> s;
		int ok = 1;
		for (int i = 0; i < length; i++)
		{
			char c = str.at(i);
			if (c == ' ')
				continue;
			if (c == '(' || c == '[')
			{
				s.push(c);
			}
			else if (c == ')' || c == ']')
			{
				if (s.size() == 0)
				{
					ok = 0;
					break;
				}
				else
				{
					char cc = s.top();
					s.pop();
					if (c == ')')
					{
						if (cc != '(')
						{
							ok = 0;
							break;
						}
					}
					else
					{
						if (cc != '[')
						{
							ok = 0;
							break;
						}
					}
				}
			}
		}
		if (s.size() != 0)
			ok = 0;

		if (ok)
			cout << yes << endl;
		else
			cout << no << endl;
	}
}

  

posted on 2017-05-16 13:02  好吧,就是菜菜  阅读(226)  评论(0编辑  收藏  举报