您的位置:首页 > 其它

(平衡的括号)Parentheses Balance UVA - 673

2018-02-14 17:48 453 查看
题目链接:Parentheses Balance UVA - 673

思路:栈的应用。。。。错都在回车!!!

代码:

#include<iostream>
#include<string>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#include<stack>
#include<map>
#include<iomanip>
#define ll long long

using namespace std;
const int maxn = 100 + 5;

int main()
{
string s;
int t;
scanf("%d",&t);
getchar();  //吃掉回车。。。。
while(t--) {
getline(cin, s);
stack<char> c;
bool f = true;
if(s.length() == 0) {
puts("Yes");
continue;
}
for(int i = 0; i < s.length(); i++) {
if(s[i] == '('||s[i] == '[') c.push(s[i]);
if(s[i] == ')') {
if(c.empty()) { f = false; break;}
else if(c.top() == '(') c.pop();
else { f = false; break;}
}
if(s[i] == ']') {
if(c.empty()) { f = false; break;}
else if(c.top() == '[') c.pop();
else { f = false; break;}
}
}
if(!c.empty() || !f) puts("No");
else puts("Yes");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: