首页 > 代码库 > UVA-673-栈-水题

UVA-673-栈-水题

题意:

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

#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-栈-水题