B - Parentheses Balance (UVA - 673)

- 题目大意

给出两个字符()【】,然后根据给的条件来判断。

- 解题思路

根据给的三个条件,然后利用栈来处理,对于暂时没有后括号匹配的前括号压入栈,遇到后括号时看栈顶的前括号与其是否匹配,如果匹配则弹出该前括号。还要判断下最后栈里面是不是有剩下的没有匹配的前括号。(注意空字符也行)

- 代码

#include<iostream>
#include<stack>
#include<cstring>

using namespace std;

int main()
{
	int x;
	char c[200];
	cin >> x;
	getchar();
	while (x--)
	{
		stack<char>num;
		gets(c);
		if (strcmp(c, "\n") == 0)
		{
			cout << "Yes" << endl;
			continue;
		}
		int a = strlen(c);
		for (int i = 0; i<a; i++)
		{
 		   if (c[i] == ‘(‘||c[i]==‘[‘)
			{
				num.push(c[i]);
				continue;
			}
		   else if (num.empty())
		   {
			   num.push(c[i]);
		   }
		   else if (c[i] == ‘)‘&&num.top()==‘(‘|| c[i] == ‘]‘&&num.top() == ‘[‘)
				{
					num.pop();
				}
				else
				{
					num.push(c[i]);
					break;
				}
			}

		if (num.empty())
			cout << "Yes" << endl;
		else
		   cout << "No" << endl;

	}

	return 0;
}

  

原文地址:https://www.cnblogs.com/alpacadh/p/8438480.html

时间: 2024-08-10 11:04:01

B - Parentheses Balance (UVA - 673)的相关文章

并查集(UVA 1106)

POINT: 把每个元素看成顶点,则一个简单化合物就是一条无向边,若存在环(即k对组合中有k种元素),则危险,不应该装箱,反之,装箱: 用一个并查集维护连通分量集合,每次得到一种化合物(x, y)时检查x, y是否在同一集合中,如果是,拒绝,反之接受. 并查集 并查集是一种树型的数据结构,用于处理一些不相交集合(Disjoint Sets)的合并及查询问题.常常在使用中以森林来表示. 集就是让每个元素构成一个单元素的集合,也就是按一定顺序将属于同一组的元素所在的集合合并. -----------

逆向+两次bfs(UVA 1599)

为什么都说简单好想咧.坦白从宽看了人家的代码,涨了好多姿势,, http://blog.csdn.net/u013382399/article/details/38227917 被一个细节坑了.. 2147483647是0x7fffffff啊啊啊,7个f!!! 1 #include <iostream> 2 #include <sstream> 3 #include <cstdio> 4 #include <cstring> 5 #include <c

Carmichael Numbers(Uva 10006)

  Carmichael Numbers An important topic nowadays in computer science is cryptography. Some people even think that cryptography is the only important field in computer science, and that life would not matter at all without cryptography. Alvaro is one

UVA - 673 - Parentheses Balance (栈的应用!)

UVA - 673 Parentheses Balance Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description  Parentheses Balance  You are given a string consisting of parentheses () and []. A string of this type is said to be co

UVa 673 Parentheses Balance(栈的使用)

 栈 Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Description You are given a string consisting of parentheses () and []. A string of this type is said to be correct: (a) if it is the empty string (b) if A and B are correct, A

UVA 637 Parentheses Balance(栈)

题目代号:HDU 1237 题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=103&page=show_problem&problem=614 题目原文: Parentheses Balance You are given a string consisting of parentheses () and []. A string of this type

D - Send a Table (UVA - 10820)

- 题目大意 给出一个n,求从1~n中任意两个数互质的对数为多少,(a,b)和(b,a)算两对. - 解题思路 构造一个欧拉函数的方法,然后用一个数组去存储下标,因为(a,b)和(b,a)算两对,所以每一个都加两倍,但(1,1)很特殊,所以最后减一就行了. - 代码 #include<iostream> #include<cmath> #include<cstring> using namespace std; const int MAX = 600000; int p

UVA-673 Parentheses Balance(栈)

题目大意:给一个括号串,看是否匹配. 题目分析:一开始用区间DP写的,超时了... 注意:空串合法. 代码如下: # include<iostream> # include<cstdio> # include<stack> # include<cstring> # include<algorithm> using namespace std; char p[130]; stack<char>s; bool judge() { int

Minimum Sum LCM(uva 10791)

题意(就是因为读错题意而wa了一次):给一个数字n,范围在[1,2^23-1],这个n是一系列数字的最小公倍数,这一系列数字的个数至少为2 例如12,是1和12的最小公倍数,是3和4的最小公倍数,是1,2,3,4,6,12的最小公倍数,是12和12的最小公倍数……………… 那么找出一个序列,使他们的和最小,上面的例子中,他们的和分别为13,7,28,24……显然最小和为7 /* 我们很容易可以发现,将n唯一分解之后,把所有质因数乘以次数加起来就行了.比如:12=2^2*3^1,那么ans=2^2