码迷,mamicode.com
首页 > 其他好文 > 详细

UVA-673-栈-水题

时间:2017-05-16 15:00:09      阅读:101      评论:0      收藏:0      [点我收藏+]

标签:ios   line   names   cout   iostream   name   tin   node   i++   

题意:

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

#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;
	}
}

  

UVA-673-栈-水题

标签:ios   line   names   cout   iostream   name   tin   node   i++   

原文地址:http://www.cnblogs.com/shuiyonglewodezzzzz/p/6860825.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!